﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
//using Microsoft.Office.Interop.Excel;
//using Excel;

namespace TPAMainReportTools
{
    public partial class MainFrm : Form
    {
        UserInfo ui;

        #region 查询字符串
        //数据库连接字符串
        string strConnectionStringTP = "Data Source=@ServerAddress;Initial Catalog=TPAMain;Persist Security Info=True;User ID=sa;Password=856518armyman;Connect Timeout=500";
        string strConnectionStringDB = "Data Source=@ServerAddress;Initial Catalog=DefualtDataBase;Persist Security Info=True;User ID=sa;Password=856518armyman;Connect Timeout=500";
        //查询从印叠组转出的记录_tab1
        string tab1_DefualtSelectString = "select gxyz002 订单编号,gxyz003 订单项次,gxyz004 型号规格,gxyz027 A卡号,gxyz001 工单号,gxyz012 移转日期 from gxyz_table where gxyz007='印叠' and gxyz002 like '20%' and len(gxyz002)=12";
        //查询从切块转出的记录_tab1
        string tab1_qkSelectString = "select sum(gxyz030) 交下工序数量 from gxyz_table where gxyz007='切块' and gxyz001 like '@GDNo%'";
        //查询合同信息_tab1
        string tab1_htSelectString = "select hta011 合同号,htb006 合同数量 from hta_table,htb_table where hta001=htb001 and hta001=@OrderID and htb002=@OrderIndex";
        //查询半成品入库信息_tab1
        string tab1_bcrkSelectString = "select sum(rk033) 入库数量,min(rk022) 入库时间 from gxrk_table where rk017='半成品库' and rk020='Y' and rk031 like '@GDNo%'";
        //查询有无副卡_tab1
        string tab1_fkCountSelectString = "select count(*) 副卡数量 from gd_table where gd001 like '@GDNo-%'";
        //查询补投次数_tab1
        string tab1_btCountSelectString = "select count(*) 补投数量 from gd_table where gd001 like 'B%' and gd014 ='@OrderID' and gd015='@OrderIndex'";
        //查询副卡工单_tab1
        string tab1_fkSearchSelectString = "select gd001 工单号,gd034 A卡号,gd007 型号规格,gd010 开卡数量,gd002 开卡时间,gd029 开卡工序 from  gd_table where gd001 like '@GDNo-%'";
        //查询补投工单_tab1
        string tab1_btSearchSelectString = "select gd014 订单编号,gd015 订单项次,gd001 工单号,gd034 A卡号,gd007 型号规格,gd010 开卡数量,gd002 开卡时间,gd029 开卡工序 from  gd_table where gd001 like 'B%' and gd014='@OrderID' and gd015='@OrderIndex'";
        //报表名称1_tab1
        string tab1_ReportFileName1 = "工单生产情况表.rpt";
        //查询欠交
        string tab2_DefualtSelectString = "select hta011 合同号,hta004 客户名称,ku007 客户区域,htb003 型号规格,htb006 合同数量,htb010 交货期,hta029 验收方式,htb018 已出库数量,htb006-htb018 欠交数量,b.编带中数量 ,(htb006-htb018)*htb008 欠交金额,htb011 已销货数量,htb021 执行标准,htb022 补充协议,(select count(*) from htb_table where htb001=hta001) 订单总项数,(select count(*) from htb_table where htb001=hta001 and htb018<htb006) 欠交总项数,hta003 客户编号,htb032 产品单项备注,htb023 订单单项备注 from hta_table,htc_table,ku_table,htb_table left join (select gxck010 订单编号,gxck011 订单项次,SUM(gxck015) 编带中数量 from gxck_table where gxck016='C' group by gxck010,gxck011) b on b.订单编号 =htb001 and b.订单项次 =htb002 where hta001=htb001 and ku001=hta003 and htc001=hta001 and htb018<htb006 and htb019='Y'";
        //报表名称1_tab2
        string tab2_ReportFileName1 = "生产欠交报表.rpt";
        //查询欠交总数量
        string tab2_SelectTotalQuantity = "select sum(htb006-htb018) 欠交总数量 from hta_table,htb_table,htc_table,ku_table where hta001=htb001 and ku001=hta003 and htc001=hta001 and htb018<htb006 and htb019='Y'";
        //查询欠交总金额
        string tab2_SelectTotalMoney = "select sum((htb006-htb018)*htb008) 欠交总金额 from hta_table,htb_table,htc_table,ku_table where hta001=htb001 and ku001=hta003 and htc001=hta001 and htb018<htb006 and htb019='Y'";
        //查询未打印且未审核的B卡工单
        string tab3_BkSelectString = "select b.hta011 合同号,a.gd015 项次,a.gd007 型号规格,c.htb010 交货期,a.gd001 工单号,a.gd034 流转卡号,c.htb021+'|'+c.htb022 执行标准,b.hta029 验收方式,a.gd010 工单数量,a.gd036 备注,a.gd026 A卡工单号 from gd_table a,hta_table b,htb_table c where a.gd025='B' and a.gd020='' and a.gd014<>'' and a.listprinted=@PrintMark and a.gd014=c.htb001 and a.gd015=c.htb002 and c.htb001=b.hta001 order by a.gd007";
        //查询调度通知
        string tab3_TzSelectString = "select tz001 通知单号,tz004 经办人,isnull(jhb024,'预排计划') 合同号,jhb009 交货日期,left(tz011,charindex('-',tz011)-1) 质量等级,RIGHT(tz011,len(tz011)-charindex('-',tz011)) 型号规格,tz017 通知数量,tz025 执行标准,tz024 B卡工单号,gd010 开卡数量,tzbz002 通知备注,htb032 产品单项备注,Processed 已处理,prtz_table.listprinted 已打印,PrintTime 打印时间 from prtz_table left join gd_table on gd001=tz024 left join scjhb_table on jhb001=tz007 and jhb002=tz008 left join hta_table on hta001=tz009 left join htb_table htb_table on htb001=tz009 and CONVERT(varchar(10),htb002)=tz010 left join prtzbz_table on tzbz001=tz001 where tz016 like '%开B卡%' and tz019='Y'";
        //查询合同备注
        string tab3_BzSelectString = "select htc002 备注 from htc_table where htc001='@HtNo'";
        //查询A卡工单流转卡号和在半成品库货架号
        string tab3_AcardAndSelfNo = "select gxjy016 A卡号,gxjy011 货架号 from gxjy_table where gxjy007='半成品库' and gxjy001 ='@GdNo'";
        //更新B卡工单打印标记
        string tab3_UpdatePrintMark = "update gd_table set listprinted=1 where gd001='@GdNo'";
        //更新通知单打印标记
        string tab3_UpdatePrintMark2 = "update prtz_table set listprinted=1,PrintTime=GetDate() where tz001='@TzNo'";
        //报表名称1_tab3
        string tab3_ReportFileName1 = "待开B卡清单.rpt";
        //报表名称2_tab3
        string tab3_ReportFileName2 = "待开B卡欠交清单.rpt";
        //查询指定条件的出库接收确认数据
        string tab5_SelectCKJSQR = "select jsqr012 订单号,jsqr013 订单项次,jsqra018 备资料日期,jsqr026 合同号,jsqr014 型号规格,(select htb021+' '+htb022 from htb_table where htb001=jsqr012 and htb002 =jsqr013) 执行标准,jsqr009 生产批号,(select htb006 from htb_table where htb001=jsqr012 and htb002 =jsqr013) 合同数量,jsqr019 提交数量,(select gxck023 from gxck_table where gxck001=jsqr007) 筛选数量,(select gxck022 from gxck_table where gxck001=jsqr007) 淘汰率,(select gxck021 from gxck_table where gxck001=jsqr007) 生产日期,replace(replace(hta029,'下厂验',''),'由','') 验收单位 from ckjsqra_table,ckjsqr_table,hta_table where jsqra007='Y' and jsqra001=jsqr001 and jsqr012=hta001 and jsqra018 between '@从' and '@到' and jsqr026 like '%@合同号' and hta029 like '%@验收单位%下厂验%'";
        //获取已验数量
        string tab5_SelectYYSum = "select sum(jsqr019) 已验数量 from ckjsqra_table,ckjsqr_table where jsqr001=jsqra001 and jsqra007='Y' and jsqr012='@订单号' and jsqr013='@订单项次'";
        //指定时间段内订单总数
        string tab7_SelectAllContractCount = "select count(distinct htb001) from htb_table where (select hta014 from hta_table where hta001=htb001)='Y' and htb006>0";
        //指定年月订货数量
        string tab8_SelectOrderSumary = "select ku007 片区,sum(htb006) 订货数量,sum(htb006*htb008) 订货金额 from hta_table,htb_table,ku_table where hta001=htb001 and hta003=ku001 and hta014='Y' and hta002 like '@年月%' group by ku007";
        //指定时间范围订货数量
        string tab8_SelectOrderSumaryDateArea = "select ku007 片区,sum(htb006) 订货数量,sum(htb006*htb008) 订货金额 from hta_table,htb_table,ku_table where hta001=htb001 and hta003=ku001 and hta014='Y' and hta002 between '@从' and '@到' group by ku007";
        //指定年月销货数量
        string tab8_SelectXHSumary = "select sum(xhb008) 销货数量,sum(xhb008*xhb011) 销货金额 from orxha_table,orxhb_table,ku_table where xha001=xhb001 and xha013='Y' and left(xha002,6)='@年月' and ku001=xha007 and ku007='@片区' group by ku007";
        //指定时间范围销货数量
        string tab8_SelectXHSumaryDateArea = "select sum(xhb008) 销货数量,sum(xhb008*xhb011) 销货金额 from orxha_table,orxhb_table,ku_table where xha001=xhb001 and xha013='Y' and xha002 between '@从' and '@到' and ku001=xha007 and ku007='@片区' group by ku007";
        //每月欠交汇总脚本
        //CC
        string tab9_SelectCCZSL="select sum((htb006-htb018)*htb008) CC总欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%')";
        string tab9_SelectCCZXC = "select count(*) CC总欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%')";
        string tab9_SelectCCQSL = "select sum((htb006-htb018)*htb008) CC之前欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%') and htb010 between '@QDateFrom' and '@QDateTo'";
        string tab9_SelectCCQXC = "select count(*) CC之前欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%') and htb010 between '@QDateFrom' and '@QDateTo'";
        string tab9_SelectCCSL = "select sum((htb006-htb018)*htb008) CC欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%') and htb010 between '@DateFrom' and '@DateTo'";
        string tab9_SelectCCXC = "select count(*) CC欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%') and htb010 between '@DateFrom' and '@DateTo'";
        string tab9_SelectCCHSL = "select sum((htb006-htb018)*htb008) CC之后欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%') and htb010 >= '@HDate'";
        string tab9_SelectCCHXC = "select count(*) CC之后欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'CC-%') and htb010 >= '@HDate'";
        //一般七专
        string tab9_SelectGZSL = "select sum((htb006-htb018)*htb008) 一般七专总欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%')";
        string tab9_SelectGZXC = "select count(*) 一般七专总欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%')";
        string tab9_SelectGQSL = "select sum((htb006-htb018)*htb008) 一般七专之前欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%') and htb010 between '@QDateFrom' and '@QDateTo'";
        string tab9_SelectGQXC = "select count(*) 一般七专之前欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%') and htb010 between '@QDateFrom' and '@QDateTo'";
        string tab9_SelectGSL = "select sum((htb006-htb018)*htb008) 一般七专欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%') and htb010 between '@DateFrom' and '@DateTo'";
        string tab9_SelectGXC = "select count(*) 一般七专欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%') and htb010 between '@DateFrom' and '@DateTo'";
        string tab9_SelectGHSL = "select sum((htb006-htb018)*htb008) 一般七专之后欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%') and htb010 >= '@HDate'";
        string tab9_SelectGHXC = "select count(*) 一般七专之后欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%') and htb010 >= '@HDate'";
        //普军
        string tab9_SelectJZSL = "select sum((htb006-htb018)*htb008) 普军总欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%')";
        string tab9_SelectJZXC = "select count(*) 普军总欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%')";
        string tab9_SelectJQSL = "select sum((htb006-htb018)*htb008) 普军之前欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%') and htb010 between '@QDateFrom' and '@QDateTo'";
        string tab9_SelectJQXC = "select count(*) 普军之前欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%') and htb010 between '@QDateFrom' and '@QDateTo'";
        string tab9_SelectJSL = "select sum((htb006-htb018)*htb008) 普军欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%') and htb010 between '@DateFrom' and '@DateTo'";
        string tab9_SelectJXC = "select count(*) 普军欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%') and htb010 between '@DateFrom' and '@DateTo'";
        string tab9_SelectJHSL = "select sum((htb006-htb018)*htb008) 普军之后欠交金额 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%') and htb010 >= '@HDate'";
        string tab9_SelectJHXC = "select count(*) 普军之后欠项次 from htb_table,hta_table where hta001=htb001 and htb018<htb006 and hta014='Y' and hta026='603车间' and (htb003 like 'J-%' or htb003 like 'M-%') and htb010 >= '@HDate'";
        //年度产品出入库提交汇总
        //七专
        string tab10_SelectQzrksl = "select sum(rk018) 七专入库 from gxrk_table where (rk013 not like 'J-%' and rk013 not like 'M-%') and rk020='Y' and rk017='成品库' and rk002 between '@Year0101' and '@Year1231'";
        string tab10_SelectQzrkwgsl = "select sum(rk018) 七专入库外购 from gxrk_table where (rk013 not like 'J-%' and rk013 not like 'M-%') and rk020='Y' and rk017='成品库' and rk002 between '@Year0101' and '@Year1231' and dbo.IsWG(rk008)=1";
        string tab10_SelectQzcksl = "select sum(gxck015) 七专出库 from gxck_table where (gxck012 not like 'J-%' and gxck012 not like 'M-%') and gxck029='成品库' and gxck002 between '@Year0101' and '@Year1231' and gxck016<>'' and gxck030 is not null";
        string tab10_SelectQzckwgsl = "select sum(gxck015) 七专出库外购 from gxck_table where (gxck012 not like 'J-%' and gxck012 not like 'M-%') and gxck029='成品库' and gxck002 between '@Year0101' and '@Year1231' and gxck016<>'' and gxck030 is not null and dbo.IsWG(gxck017)=1";
        //一般品
        string tab10_SelectYbprksl = "select sum(rk018) 一般品入库 from gxrk_table where (rk013 like 'J-%' or rk013 like 'M-%') and rk020='Y' and rk017='成品库' and rk002 between '@Year0101' and '@Year1231'";
        string tab10_SelectYbprkwgsl = "select sum(rk018) 一般品入库外购 from gxrk_table where (rk013 like 'J-%' or rk013 like 'M-%') and rk020='Y' and rk017='成品库' and rk002 between '@Year0101' and '@Year1231' and dbo.IsWG(rk008)=1";
        string tab10_SelectYbpcksl = "select sum(gxck015) 一般品出库 from gxck_table where (gxck012 like 'J-%' or gxck012 like 'M-%') and gxck029='成品库' and gxck002 between '@Year0101' and '@Year1231' and gxck016<>'' and gxck030 is not null";
        string tab10_SelectYbpckwgsl = "select sum(gxck015) 一般品出库_wg from gxck_table where (gxck012 like 'J-%' or gxck012 like 'M-%') and gxck029='成品库' and gxck002 between '@Year0101' and '@Year1231' and gxck016='Y' and gxck030 is not null and dbo.IsWG(gxck017)=1";
        //半成品
        string tab10_SelectBcprksl = "select (select sum(rk018) 半成品入库 from gxrk_table where rk017='半成品库' and rk020='Y' and rk002 between '@Year0101' and '@Year1231')+(select sum(jy015) from jywh_table where jy002 between '@Year0101' and '@Year1231' and jy014='半成品库' and jy027='') 半成品入库数量";
        string tab10_SelectBcprkwgsl = "select isnull((select sum(rk018) 半成品入库_wg from gxrk_table where rk017='半成品库' and rk020='Y' and rk002 between '@Year0101' and '@Year1231' and dbo.IsWG(rk008)=1),0)+isnull((select sum(jy015) from jywh_table where jy002 between '@Year0101' and '@Year1231' and jy014='半成品库' and jy027='' and dbo.IsWG(jy008)=1),0) 半成品外购入库数量";
        string tab10_SelectBcpcksl = "select sum(gd010) 半成品出库 from gd_table where gd003='lanqiong' and gd029='B卡发料' and gd020='Y' and gd002 between '@Year0101' and '@Year1231'";
        string tab10_SelectBcpckwgsl = "select sum(gd010) 半成品出库_wg from gd_table where gd003='lanqiong' and gd029='B卡发料' and gd020='Y' and gd002 between '@Year0101' and '@Year1231' and dbo.IsWG(gd034)=1";
        //提交
        string tab10_SelectTJ_G = "select sum(提交数量) from qzkfc where 合同号 like '%G%' and 技术条件 not like 'CAST%' and 提交时间 between '@Year-1-1' and '@Year-12-31'";
        string tab10_SelectTJ_G_wg = "select sum(提交数量) from qzkfc where 合同号 like '%G%' and 技术条件 not like 'CAST%' and 提交时间 between '@Year-1-1' and '@Year-12-31' and dbo.IsWG(批号)=1";
        string tab10_SelectTJ_G_zz_cur = "select sum(提交数量) from qzkfc where 合同号 like '%G%' and (批号 like '@SYear%' or 批号 like '@NYear%' or 批号 like 'S@NYear%')  and 技术条件 not like 'CAST%' and 提交时间 between '@Year-1-1' and '@Year-12-31' and dbo.IsWG(批号)=0";
        string tab10_SelectTJ_CC = "select sum(提交数量) from qzkfc where 技术条件 like 'CAST%' and 提交时间 between '@Year-1-1' and '@Year-12-31'";
        string tab10_SelectTJ_CC_wg = "select sum(提交数量) from qzkfc where 技术条件 like 'CAST%' and 提交时间 between '@Year-1-1' and '@Year-12-31'  and dbo.IsWG(批号)=1";
        string tab10_SelectTJ_CC_cur = "select sum(提交数量) from qzkfc where 技术条件 like 'CAST%' and (批号 like '@SYear%' or 批号 like '@NYear%' or 批号 like 'S@NYear%') and 提交时间 between '@Year-1-1' and '@Year-12-31' and dbo.IsWG(批号)=0";
        string tab10_SelectTJ_J = "select sum(提交数量) from qzkfc where 合同号 like '%J%' and 提交时间 between '@Year-1-1' and '@Year-12-31'";
        string tab10_SelectTJ_J_wg = "select sum(提交数量) from qzkfc where 合同号 like '%J%' and 提交时间 between '@Year-1-1' and '@Year-12-31'  and dbo.IsWG(批号)=1";
        string tab10_SelectTJ_J_cur = "select sum(提交数量) from qzkfc where 合同号 like '%J%' and (批号 like '@SYear%' or 批号 like '@NYear%' or 批号 like 'S@NYear%') and 提交时间 between '@Year-1-1' and '@Year-12-31' and dbo.IsWG(批号)=0";
        string tab10_SelectTJ_M = "select sum(提交数量) from qzkfc where (合同号 not like '%J%' and 合同号 not like '%G%' and 合同号 not like '% %') and 提交时间 between '@Year-1-1' and '@Year-12-31'";
        string tab10_SelectTJ_M_wg = "select sum(提交数量) from qzkfc where (合同号 not like '%J%' and 合同号 not like '%G%' and 合同号 not like '% %') and 提交时间 between '@Year-1-1' and '@Year-12-31'  and dbo.IsWG(批号)=1";
        string tab10_SelectTJ_M_cur = "select sum(提交数量) from qzkfc where (合同号 not like '%J%' and 合同号 not like '%G%' and 合同号 not like '% %') and (批号 like '@SYear%' or 批号 like '@NYear%' or 批号 like 'S@NYear%') and 提交时间 between '@Year-1-1' and '@Year-12-31' and dbo.IsWG(批号)=0";
        //生产计划统计汇总
        //一般生产计划项数、总数量
        string tab11_SelectTotal_YB = "select count(*) 总项数,sum(jhb008) 总数量 from scjhb_table,scjha_table where jha001=jhb001 and jha007='Y' and jhb003<>'' and jha002 between '@起始时间' and '@截止时间'";
        string tab11_SelectTotal_XM = "select count(*) 总项数,sum(jhb008) 总数量 from scjhb_table,scjha_table where jha001=jhb001 and jha007='Y' and jhb003='' and jha002 between '@起始时间' and '@截止时间'";
        string tab11_SelectTotal_BT = "select count(*) 总批数,sum(gd010) 总数量 from dgd_table where gd020='Y' and gd002 between '@起始时间' and '@截止时间'";
        //明细
        string tab11_SelectDetail_YB = "select jhb024 合同号,jha004 计划人,jhb001 计划单号,jhb002 计划项次,jhb003 订单编号,jhb004 订单项次,jhb005 型号规格,jhb008 计划数量,jhb009 交货日期,jhb013 已出库数量,jhb023 计划类型 from scjhb_table,scjha_table where jha001=jhb001 and jha007='Y' and jhb003<>'' and jha002 between '@起始时间' and '@截止时间'";
        string tab11_SelectDetail_XM = "select jhb024 合同号,jha004 计划人,jhb001 计划单号,jhb002 计划项次,jhb003 订单编号,jhb004 订单项次,jhb005 型号规格,jhb008 计划数量,jhb009 交货日期,jhb013 已出库数量,jhb023 计划类型 from scjhb_table,scjha_table where jha001=jhb001 and jha007='Y' and jhb003='' and jha002 between '@起始时间' and '@截止时间'";
        string tab11_SelectDetail_BT = "select gd001 补投单号,gd004 计划人,jhb024 合同号,gd007 型号规格,gd010 补投数量,gd026 工单号 from dgd_table,scjhb_table where gd027=jhb001 and gd028=jhb002 and gd020='Y' and gd002 between '@起始时间' and '@截止时间'";
        //移转单数差查询
        string tab12_SelectGXYZSC = "select gxyz007 转出工序,gxyz008 转入工序,gxyz001 工单号,gxyz004 型号规格,ycl018 尺寸,gxyz027 批号,gxyz009 转出数量,gxyz010 报废数量,gxyz030 转入数量,gxyz013 转出人,gxyz012 转出时间,gxyz025 接收人,gxyz026 接收时间 from gxyz_table left join ycl_table on ycl001=gxyz004 where gxyz025 is not null and gxyz009<>gxyz030";
        //生产计划统计
        string tab13_SelectScjh = "select jhb024 合同号,jha001 计划编号,jha002 计划日期,jha004 计划人,jhb002 计划项次,jhb005 型号规格,jhb008 计划数量,jhb009 交货期,jhb023 计划类型 from scjha_table,scjhb_table where jha001=jhb001 and jha007='Y'";
        string tab13_SelectSumScjh = "select count(*) 总项数,sum(jhb008) 总数量 from scjha_table,scjhb_table where jha001=jhb001 and jha007='Y'";
        //生产计划跟踪
        string tab14_SelectString = "select top 100 jhb024 合同号,jhb005 型号规格,htb010 交货日期,htb010 订货数量,htb018 已出库数量,htb011 销货数量,isnull(tz052,'') 重点品种,dbo.GetJhProgress(jhb001,jhb002) 计划执行情况 from scjha_table ,scjhb_table left join prtz_table on tz007=jhb001 and tz008 =jhb002 left join htb_table on htb001=jhb003 and htb002=jhb004 where jha001=jhb001 and jha007='Y'";
        //指定芯片号结余及提交清单
        string tab15_SelectJy_gx = "select gxjy001 工单号,gxjy004 型号规格,gxjy016 批号,chipsn 芯片号,gxjy007 工序,gxjy008 结余,gxjy011 货架号,gxjy015 备注 from gxjy_table left join gd_table on gd001=gxjy001 where gxjy008>0 and gxjy007<>'半成品库' and gxjy007<>'成品库' and gxjy007<>'包装检验'";
        string tab15_SelectJy_bcp = "select gxjy001 工单号,gxjy004 型号规格,gxjy016 批号,chipsn 芯片号,gxjy007 工序,gxjy008 结余,gxjy011 货架号,gxjy015 备注 from gxjy_table left join gd_table on gd001=gxjy001 where gxjy008>0 and gxjy007='半成品库'";
        string tab15_SelectJy_cp = "select gxjy001 工单号,gxjy004 型号规格,gxjy016 批号,chipsn 芯片号,gxjy007 工序,gxjy008 结余,gxjy011 货架号,gxjy015 备注 from gxjy_table left join gd_table on gd001=gxjy001 where gxjy008>0 and (gxjy007='成品库' or gxjy007='成品库')";
        string tab15_SelectTj = "select hta004 客户名称,提交时间,合同号,质量等级,型号,尺寸,特性,电压,容量,提交数量,批号,芯片号,生产日期,淘汰率,筛选数量,执行标准,isnull(gxck035,0) 已发货数量 from DefualtDataBase .dbo . QZKFC left join TPAMain .dbo .hta_table on hta001 collate Chinese_PRC_CI_AS =订单编号 left join TPAMain.dbo.gxck_table on gxck001 collate Chinese_PRC_CI_AS=出库单号";
        #endregion

        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            strConnectionStringTP = strConnectionStringTP.Replace("@ServerAddress", TPAMainReportTools.Properties.Settings.Default.ServerAddress );
            strConnectionStringDB = strConnectionStringDB.Replace("@ServerAddress", TPAMainReportTools.Properties.Settings.Default.ServerAddress);
            tab1_cbbTableSelect.SelectedIndex = 0;
            tab2_cbbSortBy.SelectedIndex = 0;
            tab2_cbbSortType.SelectedIndex = 0;
            tab2_cmbFactory.SelectedIndex = 0;
            tab2_CombSearchField.SelectedIndex = 0;
            tab2_CombSearchCompute.SelectedIndex = 0;
            tab3_cmbStatus.SelectedIndex = 1;
            tab8_cmbYear.SelectedIndex = 0;
            tab8_cmbMonth.SelectedIndex = 0;
            tab10_txtYear.Text = DateTime.Now.Year.ToString();
            tab16_cmbFactory.SelectedIndex = 0;
            timer1.Enabled = true;
            //隐藏产品预排表
            tabControl1.TabPages.Remove(tab4);
        }

        private void ShowErrMsg(string strMsg)
        {
            MessageBox.Show(strMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridView tmpdgv = null;
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(DataGridView) && c.Focused)
                {
                    tmpdgv = (DataGridView)c;
                    break;
                }
                if (c.Controls.Count > 0)
                {
                    List<DataGridView> dgvl = GetDataGridViews(c);
                    foreach (DataGridView dgv in dgvl)
                    {
                        if (dgv.Focused)
                        {
                            tmpdgv = dgv;
                            break;
                        }
                    }
                }
                if (tmpdgv != null)
                {
                    break;
                }
            }
            if (tmpdgv != null && tmpdgv.CurrentCell != null && tmpdgv.CurrentCell.Value != DBNull.Value)
            {
                Clipboard.SetText(tmpdgv.CurrentCell.Value.ToString());
            }
        }

        private List<DataGridView> GetDataGridViews(Control c)
        {
            List<DataGridView> dgvl = new List<DataGridView>();
            foreach (Control tmpc in c.Controls)
            {
                if (tmpc.GetType() == typeof(DataGridView))
                {
                    dgvl.Add((DataGridView)tmpc);
                    continue;
                }
                if (tmpc.Controls.Count > 0)
                {
                    List<DataGridView> tmpdgvl = GetDataGridViews(tmpc);
                    foreach (DataGridView tmpdgv in tmpdgvl)
                    {
                        dgvl.Add(tmpdgv);
                    }
                }
            }
            return dgvl;
        }

        private int GetInt(Object tmpobj)
        {
            try
            {
                return Convert.ToInt32(tmpobj);
            }
            catch
            {
                return 0;
            }
        }

        private double GetDouble(object tmpobj)
        {
            try
            {
                return Convert.ToDouble(tmpobj);
            }
            catch
            {
                return 0d;
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString().Trim ();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                重点用户设置ToolStripMenuItem.Visible = true;
            }
            else
            {
                重点用户设置ToolStripMenuItem.Visible = false ;
            }
            if (tabControl1.SelectedIndex == 2)
            {
                已处理ToolStripMenuItem.Visible = true;
                未处理ToolStripMenuItem.Visible = true;
            }
            else
            {
                已处理ToolStripMenuItem.Visible = false ;
                未处理ToolStripMenuItem.Visible = false ;
            }

            switch (tabControl1.SelectedIndex)
            {
                case 3:
                    if (InputBox.ShowInputBox("输入密码", "请输入密码：") != "8603")
                    {
                        MessageBox.Show("密码错误！");
                        tabControl1.SelectedIndex = 0;
                    }
                    break;
                case 6:
                    if (InputBox.ShowInputBox("输入密码", "请输入密码：") != "84866261")
                    {
                        MessageBox.Show("密码错误！");
                        tabControl1.SelectedIndex = 0;
                    }
                    break;
            }
        }

        #region 印叠工序转出报表
        private void tab1_btnSearch_Click(object sender, EventArgs e)
        {
            string tab1_SelectString = tab1_DefualtSelectString;
            string strF=tab1_dtp1 .Value .ToString ("yyyyMMdd");
            string strT=tab1_dtp2 .Value .ToString ("yyyyMMdd");
            if (tab1_cb1.Checked)
            {
                tab1_SelectString += " and gxyz012 between '" + strF + "' and '" + strT + "'";
            }
            else
            {
                tab1_SelectString += " and gxyz012='" + strF + "'";
            }
            ini_Tab1_MainData(tab1_SelectString);
            if (tab1_cb_fk.Checked)
            {
                DataTable tmpdt = (DataTable ) tab1_dgv1.DataSource;
                DataView tmpdv = tmpdt.DefaultView;
                tmpdv.RowFilter = "有副卡='true'";
                tmpdt = tmpdv.ToTable();
                tab1_dgv1.DataSource = tmpdt;
            }
            if (tab1_cb_bt.Checked)
            {
                DataTable tmpdt = (DataTable)tab1_dgv1.DataSource;
                DataView tmpdv = tmpdt.DefaultView;
                tmpdv.RowFilter = "补投次数>0";
                tmpdt = tmpdv.ToTable();
                tab1_dgv1.DataSource = tmpdt;
            }
            if (tab1_txtPrNo.Text.Trim().Length > 0)
            {
                DataTable tmpdt = (DataTable)tab1_dgv1.DataSource;
                DataView tmpdv = tmpdt.DefaultView;
                tmpdv.RowFilter = "A卡号 like '" + tab1_txtPrNo.Text.Trim()+"'";
                tmpdt = tmpdv.ToTable();
                tab1_dgv1.DataSource = tmpdt;
            }
            if (tab1_txtModel.Text.Trim().Length > 0)
            {
                DataTable tmpdt = (DataTable)tab1_dgv1.DataSource;
                DataView tmpdv = tmpdt.DefaultView;
                tmpdv.RowFilter = "型号规格 like '" + tab1_txtModel.Text.Trim() + "'";
                tmpdt = tmpdv.ToTable();
                tab1_dgv1.DataSource = tmpdt;
            }
            tab1_dgv1.AutoResizeColumns();
            tab1_dgv2.AutoResizeColumns();
            tab1_dgv3.AutoResizeColumns();
        }

        private void tab1_cb1_CheckedChanged(object sender, EventArgs e)
        {
            tab1_dtp2.Enabled = tab1_cb1.Checked;
        }

        private void ini_Tab1_MainData(string strSelectString)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("合同号");
            dt.Columns.Add("订单编号");
            dt.Columns.Add("订单项次");
            dt.Columns.Add("型号规格");
            dt.Columns.Add("合同数量",typeof (int));
            dt.Columns.Add("A卡号");
            dt.Columns.Add("切块转出数量", typeof(int));
            dt.Columns.Add("入半成品库时间");
            dt.Columns.Add("入半成品库数量", typeof(int));
            dt.Columns.Add("工单号");
            dt.Columns.Add("有副卡");
            dt.Columns.Add("补投次数",typeof (int));
            tab1_dgv1.DataSource = dt;
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpda = new SqlDataAdapter(strSelectString,conn );
            DataTable tmpdt = new DataTable();
            Tools.OpenConnection(conn);
            tmpda.Fill(tmpdt);
            Tools.CloseConnection(conn);
            if (tmpdt.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    DataRow r = dt.NewRow();
                    r["订单编号"] = tmpdt.Rows[i]["订单编号"];
                    r["订单项次"] = tmpdt.Rows[i]["订单项次"];
                    r["型号规格"] = tmpdt.Rows[i]["型号规格"];
                    r["A卡号"] = tmpdt.Rows[i]["A卡号"];
                    r["工单号"] = tmpdt.Rows[i]["工单号"];
                    if (r["订单编号"] != DBNull.Value && r["订单项次"] != DBNull.Value)
                    {
                        //查询合同号、合同数量
                        tmpda = new SqlDataAdapter(tab1_htSelectString.Replace("@OrderID", r["订单编号"].ToString()).Replace("@OrderIndex", r["订单项次"].ToString()), conn);
                        DataTable tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (tmpdt2.Rows.Count > 0)
                        {
                            r["合同号"] = tmpdt2.Rows[0]["合同号"];
                            r["合同数量"] = tmpdt2.Rows[0]["合同数量"];
                        }
                        //查询切块转出数量
                        tmpda = new SqlDataAdapter(tab1_qkSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                        tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (tmpdt2.Rows.Count > 0)
                        {
                            r["切块转出数量"] = tmpdt2.Rows[0]["交下工序数量"];
                        }
                        //查询半成品入库数量
                        tmpda = new SqlDataAdapter(tab1_bcrkSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                        tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (tmpdt2.Rows.Count > 0)
                        {
                            r["入半成品库数量"] = tmpdt2.Rows[0]["入库数量"];
                            r["入半成品库时间"] = tmpdt2.Rows[0]["入库时间"];
                        }
                        //查询有无副卡
                        tmpda = new SqlDataAdapter(tab1_fkCountSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                        tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (int.Parse (tmpdt2.Rows[0]["副卡数量"].ToString ()) == 0)
                        {
                            r["有副卡"] = false ;
                        }
                        else
                        {
                            r["有副卡"] = true ;
                        }
                        //查询补投次数
                        tmpda = new SqlDataAdapter(tab1_btCountSelectString.Replace("@OrderID", r["订单编号"].ToString()).Replace ("@OrderIndex",r["订单项次"].ToString ()), conn);
                        tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        r["补投次数"] = int.Parse(tmpdt2.Rows[0]["补投数量"].ToString());
                    }
                    dt.Rows.Add(r);
                }
                if (tab1_cb_fk.Checked)
                {
                    
                }
                tab1_dgv1.AutoResizeColumns();
            }
        }

        private void ini_Tab1_fkData(string strGDNo)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("工单号");
            dt.Columns.Add("A卡号");
            dt.Columns.Add("型号规格");
            dt.Columns.Add("开卡数量", typeof(int));
            dt.Columns.Add("开卡时间");
            dt.Columns.Add("开卡工序");
            dt.Columns.Add("切块转出数量", typeof(int));
            dt.Columns.Add("入半成品库时间");
            dt.Columns.Add("入半成品库数量", typeof(int));
            tab1_dgv2.DataSource = dt;
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpda = new SqlDataAdapter(tab1_fkSearchSelectString .Replace ("@GDNo",strGDNo ), conn);
            DataTable tmpdt = new DataTable();
            Tools.OpenConnection(conn);
            tmpda.Fill(tmpdt);
            Tools.CloseConnection(conn);
            if (tmpdt.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    DataRow r = dt.NewRow();
                    r["工单号"] = tmpdt.Rows[i]["工单号"];
                    r["A卡号"] = tmpdt.Rows[i]["A卡号"];
                    r["型号规格"] = tmpdt.Rows[i]["型号规格"];
                    r["开卡数量"] = tmpdt.Rows[i]["开卡数量"];
                    r["开卡时间"] = tmpdt.Rows[i]["开卡时间"];
                    r["开卡工序"] = tmpdt.Rows[i]["开卡工序"];
                    //查询切块转出数量
                    tmpda = new SqlDataAdapter(tab1_qkSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                    DataTable tmpdt2 = new DataTable();
                    Tools.OpenConnection(conn);
                    tmpda.Fill(tmpdt2);
                    Tools.CloseConnection(conn);
                    if (tmpdt2.Rows.Count > 0)
                    {
                        r["切块转出数量"] = tmpdt2.Rows[0]["交下工序数量"];
                    }
                    //查询半成品入库数量
                    tmpda = new SqlDataAdapter(tab1_bcrkSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                    tmpdt2 = new DataTable();
                    Tools.OpenConnection(conn);
                    tmpda.Fill(tmpdt2);
                    Tools.CloseConnection(conn);
                    if (tmpdt2.Rows.Count > 0)
                    {
                        r["入半成品库数量"] = tmpdt2.Rows[0]["入库数量"];
                        r["入半成品库时间"] = tmpdt2.Rows[0]["入库时间"];
                    }
                    dt.Rows.Add(r);
                }
                tab1_dgv2.AutoResizeColumns();
            }
        }

        private void ini_Tab1_btData(string strOrderID,string strOrderIndex)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("合同号");
            dt.Columns.Add("订单编号");
            dt.Columns.Add("订单项次");
            dt.Columns.Add("型号规格");
            dt.Columns.Add("合同数量",typeof (int));
            dt.Columns.Add("A卡号");
            dt.Columns.Add("切块转出数量", typeof(int));
            dt.Columns.Add("入半成品库时间");
            dt.Columns.Add("入半成品库数量", typeof(int));
            dt.Columns.Add("工单号");
            tab1_dgv3.DataSource = dt;
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpda = new SqlDataAdapter(tab1_btSearchSelectString.Replace ("@OrderID",strOrderID ).Replace ("@OrderIndex",strOrderIndex ), conn);
            DataTable tmpdt = new DataTable();
            Tools.OpenConnection(conn);
            tmpda.Fill(tmpdt);
            Tools.CloseConnection(conn);
            if (tmpdt.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    DataRow r = dt.NewRow();
                    r["订单编号"] = tmpdt.Rows[i]["订单编号"];
                    r["订单项次"] = tmpdt.Rows[i]["订单项次"];
                    r["型号规格"] = tmpdt.Rows[i]["型号规格"];
                    r["A卡号"] = tmpdt.Rows[i]["A卡号"];
                    r["工单号"] = tmpdt.Rows[i]["工单号"];
                    if (r["订单编号"] != DBNull.Value && r["订单项次"] != DBNull.Value)
                    {
                        //查询合同号、合同数量
                        tmpda = new SqlDataAdapter(tab1_htSelectString.Replace("@OrderID", r["订单编号"].ToString()).Replace("@OrderIndex", r["订单项次"].ToString()), conn);
                        DataTable tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (tmpdt2.Rows.Count > 0)
                        {
                            r["合同号"] = tmpdt2.Rows[0]["合同号"];
                            r["合同数量"] = tmpdt2.Rows[0]["合同数量"];
                        }
                        //查询切块转出数量
                        tmpda = new SqlDataAdapter(tab1_qkSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                        tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (tmpdt2.Rows.Count > 0)
                        {
                            r["切块转出数量"] = tmpdt2.Rows[0]["交下工序数量"];
                        }
                        //查询半成品入库数量
                        tmpda = new SqlDataAdapter(tab1_bcrkSelectString.Replace("@GDNo", r["工单号"].ToString()), conn);
                        tmpdt2 = new DataTable();
                        Tools.OpenConnection(conn);
                        tmpda.Fill(tmpdt2);
                        Tools.CloseConnection(conn);
                        if (tmpdt2.Rows.Count > 0)
                        {
                            r["入半成品库数量"] = tmpdt2.Rows[0]["入库数量"];
                            r["入半成品库时间"] = tmpdt2.Rows[0]["入库时间"];
                        }
                    }
                    dt.Rows.Add(r);
                }
                tab1_dgv3.AutoResizeColumns();
            }
        }

        private void tab1_dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (tab1_dgv1.CurrentRow != null)
            {
                if (tab1_dgv1.CurrentRow.Cells["tab1_c10"].Value != DBNull.Value && tab1_dgv1.CurrentRow.Cells["tab1_c10"].Value.ToString().Trim().Length > 0)
                {
                    if (Convert.ToBoolean(tab1_dgv1.CurrentRow.Cells["tab1_c11"].Value))
                    {
                        ini_Tab1_fkData(tab1_dgv1.CurrentRow.Cells["tab1_c10"].Value.ToString().Trim());
                    }
                    else
                    {
                        tab1_dgv2.DataSource =null;
                    }
                    if (Convert.ToInt32 (tab1_dgv1.CurrentRow.Cells["tab1_c12"].Value)>0)
                    {
                        ini_Tab1_btData(tab1_dgv1.CurrentRow.Cells["tab1_c2"].Value.ToString().Trim(), tab1_dgv1.CurrentRow.Cells["tab1_c3"].Value.ToString().Trim());
                    }
                    else
                    {
                        tab1_dgv3.DataSource = null;
                    }
                }
            }
        }

        private void tab1_btnPrint_Click(object sender, EventArgs e)
        {
            switch (tab1_cbbTableSelect.Text)
            {
                case "主表":
                    if (tab1_dgv1.SelectedRows.Count > 0)
                    {
                        try
                        {
                            tab1_ds1 tmpds = tab1_iniReportData(tab1_dgv1.SelectedRows);
                            ReportDocument rd = new ReportDocument();
                            string strTmpPath = Application.StartupPath;
                            if (!strTmpPath.EndsWith("\\"))
                            {
                                strTmpPath += "\\";
                            }
                            rd.Load(strTmpPath + tab1_ReportFileName1);
                            rd.SetDataSource(tmpds);
                            ReportPreview newReportPreview = new ReportPreview(rd);
                            newReportPreview.ShowDialog();
                        }
                        catch (Exception ex)
                        {
                            ShowErrMsg(ex.Message);
                        }
                    }
                    break;
                case "补投表":
                    if (tab1_dgv1.SelectedRows.Count > 0)
                    {
                        try
                        {
                            tab1_ds1 tmpds = tab1_iniReportData(tab1_dgv1.SelectedRows);
                            ReportDocument rd = new ReportDocument();
                            string strTmpPath = Application.StartupPath;
                            if (!strTmpPath.EndsWith("\\"))
                            {
                                strTmpPath += "\\";
                            }
                            rd.Load(strTmpPath + tab1_ReportFileName1);
                            rd.SetDataSource(tmpds);
                            ReportPreview newReportPreview = new ReportPreview(rd);
                            newReportPreview.ShowDialog();
                        }
                        catch (Exception ex)
                        {
                            ShowErrMsg(ex.Message);
                        }
                    }
                    break;
            }
        }



        private tab1_ds1 tab1_iniReportData(DataGridViewSelectedRowCollection dgsrc)
        {
            tab1_ds1 tmpds = new tab1_ds1();
            for (int i = dgsrc.Count - 1; i >= 0; i--)
            {
                DataRow r = tmpds.MainData.NewRow();
                r["合同号"] = dgsrc[i].Cells["tab1_c1"].Value;
                r["型号规格"] = dgsrc[i].Cells["tab1_c4"].Value;
                r["A卡号"] = dgsrc[i].Cells["tab1_c6"].Value;
                r["合同数量"] = dgsrc[i].Cells["tab1_c5"].Value;
                r["切块数量"] = dgsrc[i].Cells["tab1_c7"].Value;
                r["入半成品库数量"] = dgsrc[i].Cells["tab1_c9"].Value;
                tmpds.MainData.Rows.Add(r);
            }
            return tmpds;
        }
        #endregion

        #region 欠交报表
        private tab2_ds1 tab2_iniReportData(DataGridViewSelectedRowCollection dgsrc)
        {
            tab2_ds1 tmpds = new tab2_ds1();
            for (int i = dgsrc.Count - 1; i >= 0; i--)
            {
                DataRow r = tmpds.MainData.NewRow();
                r["合同号"] = dgsrc[i].Cells["tab2_c1"].Value;
                r["客户名称"] = dgsrc[i].Cells["tab2_c6"].Value;
                r["型号规格"] = dgsrc[i].Cells["tab2_c2"].Value;
                r["合同数量"] = dgsrc[i].Cells["tab2_c3"].Value;
                r["交货期"] = dgsrc[i].Cells["tab2_c4"].Value;
                r["已出库数量"] = dgsrc[i].Cells["tab2_c5"].Value;
                r["订单总项数"] = dgsrc[i].Cells["tab2_c14"].Value;
                r["欠交总项数"] = dgsrc[i].Cells["tab2_c13"].Value;
                tmpds.MainData.Rows.Add(r);
            }
            if (tmpds.MainData.Rows.Count % 37 == 0)
            {
                tmpds.MainData.Rows.Add(tmpds.MainData.NewRow());
            }
            if (tmpds.MainData.Rows.Count > 0)
            {
                string strTmpClass = string.Empty;
                for (int k = 0; k < tab2_clbLevel.CheckedItems.Count; k++)
                {
                    strTmpClass += tab2_clbLevel.CheckedItems[k].ToString();
                    if (k < tab2_clbLevel.CheckedItems.Count - 1)
                    {
                        strTmpClass += ";";
                    }
                }
                tmpds.MainData.Rows[tmpds .MainData .Rows.Count -1]["查询条件_质量等级"] = strTmpClass;
                if (tab2_chkjhq.Checked)
                {
                    tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_交货期"] = tab2_dtpFrom.Value.ToString("yyyyMMdd") + "至" + tab2_dtpTo.Value.ToString("yyyyMMdd");
                }
                else
                {
                    tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_交货期"] = string.Empty;
                }
                if (tab2_chkdhq.Checked)
                {
                    tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_订货期"] = tab2_dtpFrom2.Value.ToString("yyyyMMdd") + "至" + tab2_dtpTo2.Value.ToString("yyyyMMdd");
                }
                else
                {
                    tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_订货期"] = string.Empty;
                }
                tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_查询方案"] = tab2_cmbQueryProject.Text;
                if (tab2_lstQueryKey .Items.Count >0)
                {
                    string strTmp = string.Empty;
                    for (int k = 0; k < tab2_lstQueryKey.Items.Count; k++)
                    {
                        strTmp += tab2_lstQueryKey.Items.ToString();
                        if (k < tab2_lstQueryKey.Items.Count - 1)
                        {
                            strTmp += ";";
                        }
                    }
                    tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_其他条件"] = strTmp; ;
                }
                tmpds.MainData.Rows[tmpds.MainData.Rows.Count - 1]["查询条件_排序"] = tab2_cbbSortBy.Text + "," + tab2_cbbSortType.Text;
            }
            return tmpds;
        }

        private void tab2_btnConfigQueryProject_Click(object sender, EventArgs e)
        {
            frmConfigQueryProject_qj newFrmConfigQueryProject_qj = new frmConfigQueryProject_qj();
            newFrmConfigQueryProject_qj.ShowDialog();
        }

        private void tab2_btnSearch_Click(object sender, EventArgs e)
        {
            string strSelectString=tab2_DefualtSelectString ;
            string strTmpSelectTotalQuantity = tab2_SelectTotalQuantity;
            string strTmpSelectTotalMoney = tab2_SelectTotalMoney;
            string strModelFilter=string.Empty ;
            //处理质量等级筛选
            if (tab2_clbLevel.CheckedItems.Count > 0)
            {
                if (tab2_clbLevel.CheckedItems.Count > 1)
                {
                    strModelFilter += "(";
                }
                for (int i = 0; i < tab2_clbLevel.CheckedItems.Count; i++)
                {
                    if (i>0)
                    {
                        strModelFilter +=" or ";
                    }
                    switch (tab2_clbLevel.CheckedItems[i].ToString())
                    {
                        case "七专":
                            strModelFilter += "((htb003 not like 'SAST-%' and htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%' and htb003 not like 'GJB-%' and hta011 not like '%宏科片%' and hta011 not like '%送样%') or htb003 like 'SAST-G-%')";
                            break;
                        case "CAST":
                            strModelFilter += "(htb003 like 'CC-%' and hta011 not like '%宏科片%' and hta011 not like '%送样%')";
                            break;
                        case "普军":
                            strModelFilter += "(htb003 like 'J-%' and hta011 not like '%宏科片%' and hta011 not like '%送样%')";
                            break;
                        case "工业级":
                            strModelFilter += "(htb003 like 'M-%' and hta011 not like '%宏科片%' and hta011 not like '%送样%')";
                            break;
                        case "国军标":
                            strModelFilter += "(htb003 like 'GJB-%' and hta011 not like '%宏科片%' and hta011 not like '%送样%')";
                            break;
                        case "SAST":
                            strModelFilter += "(htb003 like 'SAST-%' and htb003 not like 'SAST-G-%' and hta011 not like '%宏科片%' and hta011 not like '%送样%')";
                            break;
                        case "送例试":
                            strModelFilter += "(hta011 like '%宏科片%')";
                            break;
                        case "送样":
                            strModelFilter += "(hta011 like '%送样%')";
                            break;
                    }
                }
                if (tab2_clbLevel.CheckedItems.Count > 1)
                {
                    strModelFilter += ")";
                }
                strSelectString += " and " + strModelFilter;
                strTmpSelectTotalQuantity += " and " + strModelFilter;
                strTmpSelectTotalMoney += " and " + strModelFilter;
            }
            //处理订货期范围筛选
            if (tab2_chkdhq.Checked)
            {
                strSelectString += " and (hta024 between '" + tab2_dtpFrom2.Value.ToString("yyyy-MM-dd") + "' and '" + tab2_dtpTo2.Value.ToString("yyyy-MM-dd") + "')";
                strTmpSelectTotalQuantity += " and (hta024 between '" + tab2_dtpFrom2.Value.ToString("yyyy-MM-dd") + "' and '" + tab2_dtpTo2.Value.ToString("yyyy-MM-dd") + "')";
                strTmpSelectTotalMoney += " and (hta024 between '" + tab2_dtpFrom2.Value.ToString("yyyy-MM-dd") + "' and '" + tab2_dtpTo2.Value.ToString("yyyy-MM-dd") + "')";
            }
            //处理交货期范围筛选
            if (tab2_chkjhq.Checked)
            {
                strSelectString += " and (htb010 between '" + tab2_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab2_dtpTo.Value.ToString("yyyyMMdd") + "')";
                strTmpSelectTotalQuantity += " and (htb010 between '" + tab2_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab2_dtpTo.Value.ToString("yyyyMMdd") + "')";
                strTmpSelectTotalMoney += " and (htb010 between '" + tab2_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab2_dtpTo.Value.ToString("yyyyMMdd") + "')";
            }
            //处理查询条件
            string strTmpQueryKey = tab2_GetQueryKeys();
            strSelectString += strTmpQueryKey;
            strTmpSelectTotalQuantity += strTmpQueryKey;
            strTmpSelectTotalMoney += strTmpQueryKey;
            //处理生产车间
            string strFactory = string.Empty;
            switch (tab2_cmbFactory.SelectedIndex)
            {
                case 0:
                    strFactory = "603车间";
                    break;
                case 1:
                    strFactory = "601车间";
                    break;
            }
            strSelectString += " and hta026='" + strFactory + "'";
            strTmpSelectTotalQuantity+= " and hta026='" + strFactory + "'";
            strTmpSelectTotalMoney += " and hta026='" + strFactory + "'";
            //处理查询方案
            switch (tab2_cmbQueryProject.SelectedIndex)
            {
                case 1:
                    strSelectString += " and (hta003 like 'YZHJ%' or hta003 like '704%')";
                    strTmpSelectTotalMoney += " and (hta003 like 'YZHJ%' or hta003 like '704%')";
                    strTmpSelectTotalQuantity += " and (hta003 like 'YZHJ%' or hta003 like '704%')";
                    break;
            }
            //排序
            string strSortBy = string.Empty;
            switch (tab2_cbbSortBy.Text)
            {
                case "合同号":
                    strSortBy = "hta011";
                    break;
                case "客户名称":
                    strSortBy = "hta004";
                    break;
                case "型号规格":
                    strSortBy = "htb003";
                    break;
                case "交货期":
                    strSortBy = "htb010";
                    break;
                case "合同数量":
                    strSortBy = "htb006";
                    break;
                case "已出库数量":
                    strSortBy = "htb018";
                    break;
                case "已销货数量":
                    strSortBy = "htb011";
                    break;
            }
            string strSortType = string.Empty;
            switch (tab2_cbbSortType.Text)
            {
                case "升序":
                    strSortType = "ASC";
                    break;
                case  "降序":
                    strSortType = "DESC";
                    break;
            }
            strSelectString += " order by " + strSortBy + " " + strSortType;
            DataTable dt = new DataTable();
            dt.Columns.Add("合同号");
            dt.Columns.Add("客户名称");
            dt.Columns.Add("客户区域");
            dt.Columns.Add("型号规格");
            dt.Columns.Add("合同数量",typeof (int));
            dt.Columns.Add("交货期");
            dt.Columns.Add("已出库数量",typeof (int));
            dt.Columns.Add("欠交数量", typeof(int));
            dt.Columns.Add("编带中数量", typeof(int));
            dt.Columns.Add("欠交金额", typeof(double ));
            dt.Columns.Add("已销货数量", typeof(int));
            dt.Columns.Add("订单总项数", typeof(int));
            dt.Columns.Add("欠交总项数", typeof(int));
            dt.Columns.Add("客户编号");
            object objTotalQuantity;
            object objTotalMoney;
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlCommand cmd1 = new SqlCommand(strTmpSelectTotalQuantity ,conn );
            cmd1.CommandTimeout = 120;
            SqlCommand cmd2 = new SqlCommand(strTmpSelectTotalMoney ,conn );
            cmd2.CommandTimeout = 120;
            SqlDataAdapter tmpda = new SqlDataAdapter(strSelectString, conn);
            tmpda.SelectCommand.CommandTimeout = 120;
            Tools.OpenConnection(conn);
            tmpda.Fill(dt);
            objTotalQuantity =cmd1.ExecuteScalar ();
            objTotalMoney =cmd2.ExecuteScalar ();
            Tools.CloseConnection(conn);
            tab2_dgv1.DataSource = dt;
            tab2_dgv1.Columns["客户编号"].Visible = false;
            tab2_tbTotalRecord.Text = dt.Rows.Count.ToString();
            tab2_tbTotalQuantity .Text =GetString (GetInt (objTotalQuantity ));
            tab2_tbTotalMoney .Text =GetString (GetDouble  (objTotalMoney ));
            if (!TPAMainReportTools.Properties.Settings.Default.tab2_ShowCustomerNames)
            {
                tab2_dgv1.Columns["tab2_c6"].Visible = false;
                tab2_dgv1.Columns["tab2_c10"].Visible = false;
            }
            tab2_dgv1.AutoResizeColumns();
            if (tab2_dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < tab2_dgv1.Rows.Count; i++)
                {
                    if (GeneralTools.GetInt(tab2_dgv1.Rows[i].Cells["tab2_c13"].Value, 0) == 1)
                    {
                        tab2_dgv1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                    }
                }
            }
            if (tab2_chkShowVIP.Checked)
            {
                SqlTools sqt=new SqlTools (GeneralTools .GetConnectionString ("DefualtDatabase"));
                DataTable tmpdt = sqt.GetData("select CustID from VIPSettings where Operator='" + ui.UserName + "'");
                if (tmpdt.Rows.Count > 0&&tab2_dgv1 .Rows.Count >0)
                {
                    for (int i = 0; i < tab2_dgv1.Rows.Count; i++)
                    {
                        for (int k = 0; k < tmpdt.Rows.Count; k++)
                        {
                            if (tab2_dgv1.Rows[i].Cells["客户编号"].Value.Equals(tmpdt.Rows[k]["CustID"]))
                            {
                                tab2_dgv1.Rows[i].Cells["tab2_c1"].Style.BackColor = Color.Pink;
                            }
                        }
                    }
                }
            }
        }

        private void tab2_dgv1_Sorted(object sender, EventArgs e)
        {
            tab2_iniRowColor();
        }

        private void tab2_dgv1_DataSourceChanged(object sender, EventArgs e)
        {
            tab2_iniRowColor();
        }

        private void tab2_iniRowColor()
        {
            if (tab2_dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < tab2_dgv1.Rows.Count; i++)
                {
                    if (tab2_dgv1.Rows[i].Cells["tab2_c13"].Value.Equals("1"))
                    {
                        tab2_dgv1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                    }
                }
            }
        }

        private void tab2_btnPrint_Click(object sender, EventArgs e)
        {
            if (tab2_dgv1.SelectedRows.Count > 0)
            {
                tab2_ds1 tmpds = tab2_iniReportData(tab2_dgv1.SelectedRows);
                ReportDocument rd = new ReportDocument();
                string strTmpPath = Application.StartupPath;
                if (!strTmpPath.EndsWith("\\"))
                {
                    strTmpPath += "\\";
                }
                rd.Load(strTmpPath + tab2_ReportFileName1);
                rd.SetDataSource(tmpds);
                ReportPreview newReportPreview = new ReportPreview(rd);
                newReportPreview.ShowDialog();
            }
        }

        private void GetResources(string strFileName)
        {           
            Stream sm= Assembly.GetExecutingAssembly().GetManifestResourceStream(@"TPAMainReportTools." + strFileName);

        }

        private void tab2_btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in tab2_dgv1.Rows)
            {
                dgvr.Selected = true;
            }
        }

        private void tab2_btnSelectExcept_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in tab2_dgv1.Rows)
            {
                dgvr.Selected = !dgvr.Selected;
            }
        }

        private void tab2_btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.xls)|*.xls";
            sfd.ShowDialog();
            if (sfd.FileName.Length > 0)
            {
                if (tab2_dgv1.SelectedRows.Count > 0)
                {
                    if (tab2_ExportToExcel(sfd.FileName, tab2_dgv1.SelectedRows))
                    {
                        MessageBox.Show("Excel文件导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowErrMsg("Excel文件导出失败！");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择要导出的记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning );
                }
            }
        }

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        private bool tab2_ExportToExcel(string strTmpFileName,DataGridViewSelectedRowCollection tmpdgvsrc)
        {
            if (tmpdgvsrc.Count == 0)
            {
                return false;
            }
            try
            {
                Excel._Application tmpapp = new Excel.Application();
                Excel.Workbook tmpwb = tmpapp.Workbooks.Add(true);
                Excel.Worksheet tmpws = (Excel.Worksheet)tmpwb.Worksheets[1];

                tmpws.Name = "生产欠交报表";
                Excel.Range tmprange;
                if (Properties.Settings.Default.tab2_ShowCustomerNames)
                {
                    //标题
                    tmprange = tmpws.get_Range("A1", "M1");
                    tmprange.Merge(0);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Name = "黑体";
                    tmprange.Font.Size = 15;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "生产欠交报表";
                    //列头
                    tmprange = (Excel.Range)tmpws.Cells[2, 1];
                    tmprange.ColumnWidth = 18;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "合同号";
                    tmprange = (Excel.Range)tmpws.Cells[2, 2];
                    tmprange.ColumnWidth = 35;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "客户名称";
                    tmprange = (Excel.Range)tmpws.Cells[2, 3];
                    tmprange.ColumnWidth = 35;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "客户区域";
                    tmprange = (Excel.Range)tmpws.Cells[2, 4];
                    tmprange.ColumnWidth = 70;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "型号规格";
                    tmprange = (Excel.Range)tmpws.Cells[2, 5];
                    tmprange.ColumnWidth = 9;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "合同数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 6];
                    tmprange.ColumnWidth = 9;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "交货期";
                    tmprange = (Excel.Range)tmpws.Cells[2, 7];
                    tmprange.ColumnWidth = 12;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "验收方式";
                    tmprange = (Excel.Range)tmpws.Cells[2, 8];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "已出库数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 9];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "欠交数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 10];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "欠交金额";
                    tmprange = (Excel.Range)tmpws.Cells[2, 11];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "已销货数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 12];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "编带中数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 13];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "欠交总项数";
                    //内容
                    #region 老算法
                    //for (int i = tmpdgvsrc.Count - 1; i >= 0; i--)
                    //{
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 1];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c1"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 2];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c6"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 3];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c10"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 4];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c2"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 5];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c3"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 6];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c4"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 7];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c11"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 8];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c5"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 9];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c8"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 10];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c9"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 11];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c7"].Value;
                    //}
                    #endregion
                    #region 新算法
                    object[,] tmpobj = new object[tmpdgvsrc.Count, 13];
                    for (int i = tmpdgvsrc.Count - 1; i >= 0; i--)
                    {
                        tmpobj[i, 0] = tmpdgvsrc[i].Cells["tab2_c1"].Value;
                        tmpobj[i, 1] = tmpdgvsrc[i].Cells["tab2_c6"].Value;
                        tmpobj[i, 2] = tmpdgvsrc[i].Cells["tab2_c10"].Value;
                        tmpobj[i, 3] = tmpdgvsrc[i].Cells["tab2_c2"].Value;
                        tmpobj[i, 4] = tmpdgvsrc[i].Cells["tab2_c3"].Value;
                        tmpobj[i, 5] = tmpdgvsrc[i].Cells["tab2_c4"].Value;
                        tmpobj[i, 6] = tmpdgvsrc[i].Cells["tab2_c11"].Value;
                        tmpobj[i, 7] = tmpdgvsrc[i].Cells["tab2_c5"].Value;
                        tmpobj[i, 8] = tmpdgvsrc[i].Cells["tab2_c8"].Value;
                        tmpobj[i, 9] = tmpdgvsrc[i].Cells["tab2_c9"].Value;
                        tmpobj[i, 10] = tmpdgvsrc[i].Cells["tab2_c7"].Value;
                        tmpobj[i, 11] = tmpdgvsrc[i].Cells["tab2_c14"].Value;
                        tmpobj[i, 12] = tmpdgvsrc[i].Cells["tab2_c13"].Value;
                    }
                    tmprange = tmpws.get_Range("A3", tmpws.Cells[tmpdgvsrc.Count + 2, 13]);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpobj;
                    #endregion
                }
                else
                {
                    //标题
                    tmprange = tmpws.get_Range("A1", "H1");
                    tmprange.Merge(0);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Name = "黑体";
                    tmprange.Font.Size = 15;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "生产欠交报表";
                    //列头
                    tmprange = (Excel.Range)tmpws.Cells[2, 1];
                    tmprange.ColumnWidth = 18;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "合同号";
                    tmprange = (Excel.Range)tmpws.Cells[2, 2];
                    tmprange.ColumnWidth = 35;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "型号规格";
                    tmprange = (Excel.Range)tmpws.Cells[2, 3];
                    tmprange.ColumnWidth = 9;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "合同数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 4];
                    tmprange.ColumnWidth = 9;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "交货期";
                    tmprange = (Excel.Range)tmpws.Cells[2, 5];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "已出库数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 6];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "已销货数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 7];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "编带中数量";
                    tmprange = (Excel.Range)tmpws.Cells[2, 8];
                    tmprange.ColumnWidth = 11;
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Font.Bold = true;
                    tmprange.Value2 = "欠交总项数";
                    //内容
                    #region 老算法
                    //for (int i = tmpdgvsrc.Count - 1; i >= 0; i--)
                    //{
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 1];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c1"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 2];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c2"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 3];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c3"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 4];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c4"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 5];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c5"].Value;
                    //    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvsrc.Count - i, 6];
                    //    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    //    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    //    tmprange.Value2 = tmpdgvsrc[i].Cells["tab2_c7"].Value;
                    //}
                    #endregion
                    #region 新算法
                    object[,] tmpobj = new object[tmpdgvsrc.Count, 8];
                    for (int i = tmpdgvsrc.Count - 1; i >= 0; i--)
                    {
                        tmpobj[i, 0] = tmpdgvsrc[i].Cells["tab2_c1"].Value;
                        tmpobj[i, 1] = tmpdgvsrc[i].Cells["tab2_c2"].Value;
                        tmpobj[i, 2] = tmpdgvsrc[i].Cells["tab2_c3"].Value;
                        tmpobj[i, 3] = tmpdgvsrc[i].Cells["tab2_c4"].Value;
                        tmpobj[i, 4] = tmpdgvsrc[i].Cells["tab2_c5"].Value;
                        tmpobj[i, 5] = tmpdgvsrc[i].Cells["tab2_c7"].Value;
                        tmpobj[i, 6] = tmpdgvsrc[i].Cells["tab2_c14"].Value;
                        tmpobj[i, 7] = tmpdgvsrc[i].Cells["tab2_c13"].Value;
                    }
                    tmprange = tmpws.get_Range("A3", tmpws.Cells[tmpdgvsrc.Count + 2, 8]);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpobj;
                    #endregion
                }
                tmpwb.SaveAs(strTmpFileName, Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                tmpapp.Quit();
                if (tmpapp != null)
                {
                    IntPtr t = new IntPtr(tmpapp.Hwnd);
                    int k = 0;
                    GetWindowThreadProcessId(t, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        private string tab2_GetQueryKeys()
        {
            string strTmpQueryKey = string.Empty;
            if (tab2_lstQueryKey.Items.Count > 0)
            {
                for (int i = 0; i < tab2_lstQueryKey.Items.Count; i++)
                {
                    string[] strTmpKeys = tab2_lstQueryKey.Items[i].ToString().Split(new char[] { '|' });
                    //处理运算符
                    switch (strTmpKeys[1])
                    {
                        case "包含":
                            strTmpKeys[1] = " like ";
                            break;
                        case "不包含":
                            strTmpKeys[1] = " not like ";
                            break;
                        case "等于":
                            strTmpKeys[1] = " = ";
                            break;
                        case "不等于":
                            strTmpKeys[1] = " <> ";
                            break;
                        case "大于":
                            strTmpKeys[1] = " > ";
                            break;
                        case "大于等于":
                            strTmpKeys[1] = " >= ";
                            break;
                        case "小于":
                            strTmpKeys[1] = " < ";
                            break;
                        case "小于等于":
                            strTmpKeys[1] = " <= ";
                            break;
                    }
                    //处理字段
                    //协议或标准号
                    //合同备注
                    //型号规格
                    //合同号
                    //客户区域
                    //验收方式
                    //客户名称
                    //客户编号
                    switch (strTmpKeys[0])
                    {
                        case "协议或标准号":
                            strTmpQueryKey += " and (htb021" + strTmpKeys[1] + "'" + strTmpKeys[2] + "' or htb022" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "合同备注":
                            strTmpQueryKey += " and (htc002" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "型号规格":
                            strTmpQueryKey += " and (htb003" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "合同号":
                            strTmpQueryKey += " and (hta011" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "客户区域":
                            strTmpQueryKey += " and (ku007" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "验收方式":
                            strTmpQueryKey += " and (hta029" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "客户名称":
                            strTmpQueryKey += " and (hta004" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "客户编号":
                            strTmpQueryKey += " and (hta003" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                        case "欠交金额":
                            strTmpQueryKey += " and (((htb006-htb018)*htb008)" + strTmpKeys[1] + "'" + strTmpKeys[2] + "')";
                            break;
                    }
                }
            }
            return strTmpQueryKey;
        }

        private void 重点用户设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ui == null)
            {
                frmLogin newFrmLogin = frmLogin.Show();
                if (newFrmLogin.bLoginSuccess)
                {
                    ui = newFrmLogin.ui;
                }
            }
            else
            {
                frmSetVIP newFrmSetVIP = new frmSetVIP(ui);
                newFrmSetVIP.ShowDialog();
                tab2_btnSearch_Click(this, new EventArgs());
            }
        }

        private void tab2_btnAddQuery_Click(object sender, EventArgs e)
        {
            if (tab2_txtQueryKey.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入要查询的关键字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tab2_txtQueryKey.Focus();
                return;
            }
            string strTmpField = tab2_CombSearchField.Text + "|" + tab2_CombSearchCompute.Text;
            string strTmpKey = tab2_txtQueryKey.Text;
            if (tab2_lstQueryKey.FindString(strTmpField) >= 0)
            {
                tab2_lstQueryKey.Items[tab2_lstQueryKey.FindString(strTmpField)] = strTmpField + "|" + strTmpKey;
            }
            else
            {
                tab2_lstQueryKey.Items.Add(strTmpField + "|" + strTmpKey);
            }
        }

        private void tab2_lstQueryKey_DoubleClick(object sender, EventArgs e)
        {
            if (tab2_lstQueryKey.SelectedIndex >= 0)
            {
                tab2_lstQueryKey.Items.RemoveAt(tab2_lstQueryKey.SelectedIndex);
            }
        }

        private void tab2_chkShowVIP_CheckedChanged(object sender, EventArgs e)
        {
            if (tab2_chkShowVIP.Checked)
            {
                frmLogin newFrmLogin = frmLogin.Show();
                if (newFrmLogin.bLoginSuccess)
                {
                    ui = newFrmLogin.ui;
                }
                else
                {
                    tab2_chkShowVIP.Checked = false;
                }
            }
        }
        #endregion

        #region 待开B卡清单
        private void tab3_btnSearch_Click(object sender, EventArgs e)
        {
            tab3_dgv3.Columns.Clear();
            DataTable dt=new DataTable ();
            SqlConnection tmpconn= Tools.GetConnection (strConnectionStringTP );
            SqlDataAdapter tmpsda;
            if (tab3_chk1.Checked)
            {
                tmpsda =new SqlDataAdapter (tab3_BkSelectString.Replace ("@PrintMark","0") ,tmpconn );
            }
            else 
            {
                tmpsda =new SqlDataAdapter (tab3_BkSelectString.Replace ("@PrintMark","1" ),tmpconn );
            }
            Tools.OpenConnection (tmpconn );
            tmpsda .Fill (dt);
            Tools.CloseConnection (tmpconn );
            DataGridViewCheckBoxColumn dgvcbc = new DataGridViewCheckBoxColumn();
            dgvcbc.Name = "打印";
            tab3_dgv3.Columns.Add(dgvcbc);
            tab3_dgv3.DataSource = dt;
            tab3_dgv3.Columns.Add("A卡号", "A卡号");
            tab3_dgv3.Columns.Add("货架号", "货架号");
            if (tab3_dgv3.Rows.Count > 0)
            {
                Tools.OpenConnection(tmpconn);
                DataTable tmpdt;
                for (int i = 0; i < tab3_dgv3.Rows.Count; i++)
                {
                    if (tab3_dgv3.Rows[i].Cells["A卡工单号"].Value==DBNull .Value || tab3_dgv3.Rows[i].Cells["A卡工单号"].Value.ToString().Trim().Length == 0)
                    {
                        continue;
                    }
                    tmpdt = new DataTable();
                    tmpsda = new SqlDataAdapter(tab3_AcardAndSelfNo.Replace("@GdNo", tab3_dgv3.Rows[i].Cells["A卡工单号"].Value.ToString()), tmpconn);
                    tmpsda.Fill(tmpdt);
                    if (tmpdt.Rows.Count > 0)
                    {
                        tab3_dgv3.Rows[i].Cells["A卡号"].Value = tmpdt.Rows[0]["A卡号"];
                        tab3_dgv3.Rows[i].Cells["货架号"].Value = tmpdt.Rows[0]["货架号"];
                    }
                }
                Tools.CloseConnection(tmpconn);                
            }
            tab3_dgv3.AutoResizeColumns();
        }

        private void tab3_btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tab3_dgv3.Rows.Count; i++)
            {
                tab3_dgv3.Rows[i].Cells ["打印"].Value = true;
            }
        }

        private tab3_ds1 tab3_iniReportData()
        {
            tab3_ds1 tmpds = new tab3_ds1();
            for (int i =0;i< tab3_dgv3.Rows.Count ; i++)
            {
                if (!Convert.ToBoolean(tab3_dgv3.Rows[i].Cells["打印"].Value))
                {
                    continue;
                }
                DataRow r = tmpds.MainData.NewRow();
                r["B卡工单号"] = tab3_dgv3 .Rows [i].Cells["工单号"].Value;
                r["合同号"] = tab3_dgv3.Rows[i].Cells["合同号"].Value;
                r["型号规格"] = tab3_dgv3.Rows[i].Cells["型号规格"].Value;
                r["A卡号"] = tab3_dgv3.Rows[i].Cells["A卡号"].Value;
                if (TPAMainReportTools.Properties.Settings.Default.tab3_ShowBCardNo)
                {
                    r["B卡号"] = tab3_dgv3.Rows[i].Cells["流转卡号"].Value;
                }
                r["数量"] = tab3_dgv3.Rows[i].Cells["工单数量"].Value.ToString().Replace(".00", "");
                r["交货期"] = tab3_dgv3.Rows[i].Cells["交货期"].Value;
                r["执行标准"] = tab3_dgv3.Rows[i].Cells["执行标准"].Value;
                r["货架号"] = tab3_dgv3.Rows[i].Cells["货架号"].Value;
                r["验收方式"] = tab3_dgv3.Rows[i].Cells["验收方式"].Value;
                r["备注"] = tab3_dgv3.Rows[i].Cells["备注"].Value;
                tmpds.MainData.Rows.Add(r);
            }
            return tmpds;
        }

        private void tab3_btnPrint_Click(object sender, EventArgs e)
        {
            if (tab3_dgv3.Rows.Count > 0)
            {
                tab3_ds1 tmpds = tab3_iniReportData();
                ReportDocument rd = new ReportDocument();
                string strTmpPath = Application.StartupPath;
                if (!strTmpPath.EndsWith("\\"))
                {
                    strTmpPath += "\\";
                }
                rd.Load(strTmpPath + tab3_ReportFileName1);
                rd.SetDataSource(tmpds);
                if (ReportPreview.ShowReportPreview(rd))
                {
                    SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
                    SqlCommand tmpcmd;
                    Tools.OpenConnection(tmpconn);
                    for (int i = 0; i < tab3_dgv3.Rows.Count; i++)
                    {
                        if (Convert.ToBoolean(tab3_dgv3.Rows[i].Cells["打印"].Value))
                        {
                            tmpcmd = new SqlCommand(tab3_UpdatePrintMark.Replace("@GdNo", tab3_dgv3.Rows[i].Cells["工单号"].Value.ToString()), tmpconn);
                            //try
                            //{
                            tmpcmd.ExecuteNonQuery();
                            //}
                            //catch
                            //{
                            //    continue;
                            //}
                        }
                    }
                    Tools.CloseConnection(tmpconn);
                }
            }
        }

        private void tab3_dgv3_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (tab3_dgv3.CurrentRow != null)
            {
                tab3_dgv3.CurrentRow.Cells["打印"].Value = !Convert.ToBoolean(tab3_dgv3.CurrentRow.Cells["打印"].Value);
            }
        }

        private void tab3_btnFind_Click(object sender, EventArgs e)
        {
            if (tab3_txtGdNo.Text.Trim().Length > 0)
            {
                for (int i = 0; i < tab3_dgv3.Rows.Count; i++)
                {
                    if (tab3_txtGdNo.Text.Trim() == tab3_dgv3.Rows[i].Cells["工单号"].Value.ToString())
                    {
                        tab3_dgv3.CurrentCell = tab3_dgv3.Rows[i].Cells ["打印"];
                        tab3_dgv3.Rows[i].Selected = true;
                        break;
                    }
                }
            }
        }

        private void tab3_btnSelectSelected_Click(object sender, EventArgs e)
        {
            if (tab3_dgv3.SelectedRows.Count > 0)
            {
                for (int i = 0; i < tab3_dgv3.SelectedRows.Count; i++)
                {
                    tab3_dgv3.SelectedRows[i].Cells["打印"].Value = true;
                }
            }
        }

        private void tab3_btnClearAll_Click(object sender, EventArgs e)
        {
            if (tab3_dgv3.Rows.Count > 0)
            {
                for (int i = 0; i < tab3_dgv3.Rows.Count; i++)
                {
                    tab3_dgv3.Rows[i].Cells["打印"].Value = false;
                }
            }
        }

        private void tab3_btnSearch2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpsda;
            string strTmpSelectString = tab3_TzSelectString;
            if (tab3_rbtnWkk.Checked)
            {
                strTmpSelectString += " and (gd020 is null or gd020='')  and Processed=0 and jhb008>jhb013";
            }
            if (tab3_rbtnYcl.Checked)
            {
                strTmpSelectString += " and Processed=1";
            }
            if (tab3_rbtnYjc.Checked)
            {
                strTmpSelectString += " and (gd020='Y' or Processed=1)";
            }
            if (!tab3_txtContract.Text.Trim().Equals(string.Empty))
            {
                strTmpSelectString += " and jhb024 like '" + tab3_txtContract.Text.Trim() +"'";
            }
            if (!tab3_txtClass.Text.Trim().Equals(string.Empty))
            {
                strTmpSelectString += " and tz011 like '" + tab3_txtClass.Text.Trim() + "-%'";
            }
            if (!tab3_txtModel.Text.Trim().Equals(string.Empty))
            {
                strTmpSelectString += " and tz011 like '" + tab3_txtModel.Text.Trim() + "'";
            }
            switch (tab3_cmbStatus.SelectedIndex)
            {
                case 1:
                    strTmpSelectString += " and prtz_table.listprinted=0";
                    break;
                case 2:
                    strTmpSelectString += " and prtz_table.listprinted=1";
                    break;
            }
            tmpsda = new SqlDataAdapter(strTmpSelectString , tmpconn);
            Tools.OpenConnection(tmpconn);
            tmpsda.Fill(dt);
            Tools.CloseConnection(tmpconn);
            tab3_dgv2.DataSource = dt;
            tab3_dgv2.AutoResizeColumns();
            if (tab3_dgv2.DataSource != null)
            {
                tab3_dgv2.Columns[0].Frozen = true;
                tab3_dgv2.Columns[1].Frozen = true;
                tab3_dgv2.Columns[2].Frozen = true;
                tab3_dgv2.Columns[3].Frozen = true;
            }
            if (tab3_dgv2.Rows.Count > 0)
            {
                for (int i = 0; i < tab3_dgv2.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(tab3_dgv2.Rows[i].Cells["已打印"].Value))
                    {
                        tab3_dgv2.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                    }
                }
            }
        }


        private void tab3_btnPrint2_Click(object sender, EventArgs e)
        {
            if (tab3_dgv2.Rows.Count > 0)
            {
                tab3_ds2 tmpds = tab3_iniReportData2();
                ReportDocument rd = new ReportDocument();
                string strTmpPath = Application.StartupPath;
                if (!strTmpPath.EndsWith("\\"))
                {
                    strTmpPath += "\\";
                }
                rd.Load(strTmpPath + tab3_ReportFileName2);
                rd.SetDataSource(tmpds);
                if (ReportPreview.ShowReportPreview(rd))
                {
                    SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
                    SqlCommand tmpcmd;
                    Tools.OpenConnection(tmpconn);
                    if (tab3_chkPrintSelected.Checked )
                    {
                        for (int i = 0; i < tab3_dgv2.SelectedRows.Count; i++)
                        {
                            tmpcmd = new SqlCommand(tab3_UpdatePrintMark2.Replace("@TzNo", tab3_dgv2.SelectedRows[i].Cells["通知单号"].Value.ToString()), tmpconn);
                            tmpcmd.ExecuteNonQuery();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < tab3_dgv2.Rows.Count; i++)
                        {
                            tmpcmd = new SqlCommand(tab3_UpdatePrintMark2.Replace("@TzNo", tab3_dgv2.Rows[i].Cells["通知单号"].Value.ToString()), tmpconn);
                            tmpcmd.ExecuteNonQuery();
                        }
                    }
                    Tools.CloseConnection(tmpconn);
                    tab3_btnSearch2_Click(this, new EventArgs());
                }
            }
        }

        private tab3_ds2 tab3_iniReportData2()
        {
            tab3_ds2 tmpds = new tab3_ds2();
            if (tab3_chkPrintSelected.Checked)
            {
                if (tab3_dgv2.SelectedRows.Count == 0)
                {
                    return tmpds;
                }
                List<int> idx = new List<int>();
                for (int i = 0; i < tab3_dgv2.SelectedRows.Count; i++)
                {
                    idx.Add(tab3_dgv2.SelectedRows[i].Index);
                }
                idx.Sort();
                for (int i = 0; i < idx.Count; i++)
                {
                    DataRow r = tmpds.MainData.NewRow();
                    r["通知单号"] = tab3_dgv2.Rows[idx[i]].Cells["通知单号"].Value;
                    r["合同号"] = tab3_dgv2.Rows[idx[i]].Cells["合同号"].Value;
                    r["型号规格"] = tab3_dgv2.Rows[idx[i]].Cells["质量等级"].Value.ToString() + "-" + tab3_dgv2.Rows[idx[i]].Cells["型号规格"].Value.ToString();
                    r["交货期"] = tab3_dgv2.Rows[idx[i]].Cells["交货日期"].Value;
                    r["执行标准"] = tab3_dgv2.Rows[idx[i]].Cells["执行标准"].Value;
                    r["数量"] = tab3_dgv2.Rows[idx[i]].Cells["通知数量"].Value.ToString().Replace(".00", "");
                    r["备注"] = tab3_dgv2.Rows[idx[i]].Cells["通知备注"].Value;
                    r["产品单项备注"] = GetString(tab3_dgv2.Rows[idx[i]].Cells["产品单项备注"].Value);
                    tmpds.MainData.Rows.Add(r);
                }
            }
            else
            {
                for (int i = 0; i < tab3_dgv2.Rows.Count; i++)
                {
                    DataRow r = tmpds.MainData.NewRow();
                    r["通知单号"] = tab3_dgv2.Rows[i].Cells["通知单号"].Value;
                    r["合同号"] = tab3_dgv2.Rows[i].Cells["合同号"].Value;
                    r["型号规格"] = tab3_dgv2.Rows[i].Cells["质量等级"].Value.ToString() + "-" + tab3_dgv2.Rows[i].Cells["型号规格"].Value.ToString();
                    r["交货期"] = tab3_dgv2.Rows[i].Cells["交货日期"].Value;
                    r["执行标准"] = tab3_dgv2.Rows[i].Cells["执行标准"].Value;
                    r["数量"] = tab3_dgv2.Rows[i].Cells["通知数量"].Value.ToString().Replace(".00", "");
                    r["备注"] = tab3_dgv2.Rows[i].Cells["通知备注"].Value;
                    r["产品单项备注"] = GetString(tab3_dgv2.Rows[i].Cells["产品单项备注"].Value);
                    tmpds.MainData.Rows.Add(r);
                }
            }
            return tmpds;
}

        private void tab3_rbtnWkk_CheckedChanged(object sender, EventArgs e)
        {
            if (tab3_rbtnWkk.Checked)
            {
                tab3_rbtnYcl.Checked = !tab3_rbtnWkk.Checked;
                tab3_rbtnYjc.Checked = !tab3_rbtnWkk.Checked;
            }
        }

        private void tab3_rbtnYjc_CheckedChanged(object sender, EventArgs e)
        {
            if (tab3_rbtnYjc.Checked)
            {
                tab3_rbtnWkk.Checked = !tab3_rbtnYjc.Checked;
                tab3_rbtnYcl.Checked = !tab3_rbtnYjc.Checked;
            }
        }


        private void tab3_rbtnYcl_CheckedChanged(object sender, EventArgs e)
        {
            if (tab3_rbtnYcl.Checked)
            {
                tab3_rbtnWkk.Checked = !tab3_rbtnYcl.Checked;
                tab3_rbtnYjc.Checked = !tab3_rbtnYcl.Checked;
            }
        }

        private void 已处理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tab3_dgv2.SelectedRows.Count > 0)
            {
                SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
                string strUpdateTzProcessed = "update prtz_table set Processed=1 where tz001='@通知单号'";
                Tools.OpenConnection(tmpconn);
                for (int i = 0; i < tab3_dgv2.SelectedRows.Count; i++)
                {
                    SqlCommand tmpcmd = new SqlCommand(strUpdateTzProcessed.Replace("@通知单号", GetString(tab3_dgv2.SelectedRows[i].Cells["通知单号"].Value)), tmpconn);
                    tmpcmd.ExecuteNonQuery();
                }
                Tools.CloseConnection(tmpconn);
                tab3_btnSearch2_Click(this, new EventArgs());
            }
        }


        private void 未处理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tab3_dgv2.SelectedRows.Count > 0)
            {
                SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
                string strUpdateTzProcessed = "update prtz_table set Processed=0 where tz001='@通知单号'";
                Tools.OpenConnection(tmpconn);
                for (int i = 0; i < tab3_dgv2.SelectedRows.Count; i++)
                {
                    SqlCommand tmpcmd = new SqlCommand(strUpdateTzProcessed.Replace("@通知单号", GetString(tab3_dgv2.SelectedRows[i].Cells["通知单号"].Value)), tmpconn);
                    tmpcmd.ExecuteNonQuery();
                }
                Tools.CloseConnection(tmpconn);
                tab3_btnSearch2_Click(this, new EventArgs());
            }
        }

        private void tab3_btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.xls)|*.xls";
            sfd.ShowDialog();
            if (sfd.FileName.Length > 0)
            {
                if (tab3_dgv2.Rows.Count > 0)
                {
                    if (!tab3_chkPrintSelected.Checked)
                    {
                        tab3_dgv2.SelectAll();
                    }
                    if (tab3_ExportToExcel(sfd.FileName, tab3_dgv2.SelectedRows  ))
                    {
                        MessageBox.Show("Excel文件导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowErrMsg("Excel文件导出失败！");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择要导出的记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private bool tab3_ExportToExcel(string strTmpFileName, DataGridViewSelectedRowCollection tmpdgvrc)
        {
            if (tmpdgvrc.Count == 0)
            {
                return false;
            }
            try
            {
                Excel._Application tmpapp = new Excel.Application();
                Excel.Workbook tmpwb = tmpapp.Workbooks.Add(true);
                Excel.Worksheet tmpws = (Excel.Worksheet)tmpwb.Worksheets[1];

                tmpws.Name = "半成品欠交";
                Excel.Range tmprange;
                //列头
                tmprange = (Excel.Range)tmpws.Cells[1, 1];
                tmprange.ColumnWidth =14;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "通知单号";

                tmprange = (Excel.Range)tmpws.Cells[1, 2];
                tmprange.ColumnWidth = 18;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "合同号";

                tmprange = (Excel.Range)tmpws.Cells[1,3];
                tmprange.ColumnWidth = 9;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "交货日期";

                tmprange = (Excel.Range)tmpws.Cells[1, 4];
                tmprange.ColumnWidth = 9;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "质量等级";

                tmprange = (Excel.Range)tmpws.Cells[1, 5];
                tmprange.ColumnWidth = 40;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "型号规格";

                tmprange = (Excel.Range)tmpws.Cells[1, 6];
                tmprange.ColumnWidth = 9;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "通知数量";

                tmprange = (Excel.Range)tmpws.Cells[1, 7];
                tmprange.ColumnWidth = 35;
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "执行标准";

                #region 新算法
                object[,] tmpobj = new object[tmpdgvrc.Count, 7];
                for (int i =tmpdgvrc .Count -1; i>=0 ; i--)
                {
                    tmpobj[i, 0] = "'"+GetString (tmpdgvrc[i].Cells["通知单号"].Value);
                    tmpobj[i, 1] = tmpdgvrc[i].Cells["合同号"].Value;
                    tmpobj[i, 2] = tmpdgvrc[i].Cells["交货日期"].Value;
                    tmpobj[i, 3] = tmpdgvrc[i].Cells["质量等级"].Value;
                    tmpobj[i, 4] = tmpdgvrc[i].Cells["型号规格"].Value;
                    tmpobj[i, 5] = tmpdgvrc[i].Cells["通知数量"].Value;
                    tmpobj[i, 6] = tmpdgvrc[i].Cells["执行标准"].Value;
                }
                tmprange = tmpws.get_Range("A2", tmpws.Cells[tmpdgvrc.Count + 1, 7]);
                //tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Value2 = tmpobj;
                #endregion
                
                tmpwb.SaveAs(strTmpFileName, Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                tmpapp.Quit();
                if (tmpapp != null)
                {
                    IntPtr t = new IntPtr(tmpapp.Hwnd);
                    int k = 0;
                    GetWindowThreadProcessId(t, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion

        #region 产品预排表（加密8603）

        private void tab4_rdoAdd_CheckedChanged(object sender, EventArgs e)
        {
            tab4_rdoQueryEdit.Checked = !tab4_rdoAdd.Checked ;
        }

        private void tab4_rdoQueryEdit_CheckedChanged(object sender, EventArgs e)
        {
            tab4_rdoAdd.Checked = !tab4_rdoQueryEdit.Checked;
        }

        #endregion

        #region 下厂验清单打印工具

        private void tab5_btnSearch_Click(object sender, EventArgs e)
        {
            SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpsda = new SqlDataAdapter(tab5_SelectCKJSQR.Replace("@从", tab5_dtpFrom.Value.ToString("yyyy-MM-dd"))
                                                                        .Replace("@到", tab5_dtpTo.Value.ToString("yyyy-MM-dd"))
                                                                        .Replace("@合同号", tab5_txtContractNo.Text.Trim ())
                                                                        .Replace("@验收单位", tab5_txtCustomer.Text.Trim ()), tmpconn);
            DataTable tmpdt = new DataTable();
            tmpsda.SelectCommand.CommandTimeout = 0;
            Tools.OpenConnection(tmpconn);
            tmpsda.Fill(tmpdt);
            Tools.CloseConnection(tmpconn);
            tab5_dgv1.DataSource = tmpdt;
            if (tab5_dgv1.Rows.Count > 0)
            {
                SqlCommand tmpcmd;
                Tools.OpenConnection(tmpconn);
                for (int i = 0; i < tab5_dgv1.Rows.Count; i++)
                {
                    tmpcmd = new SqlCommand(tab5_SelectYYSum.Replace("@订单号", tab5_dgv1.Rows[i].Cells["订单号"].Value.ToString())
                                                            .Replace("@订单项次", tab5_dgv1.Rows[i].Cells["订单项次"].Value.ToString()), tmpconn);
                    tab5_dgv1.Rows[i].Cells["已验数量"].Value = GetInt(tmpcmd.ExecuteScalar()) - GetInt(tab5_dgv1.Rows[i].Cells["提交数量"].Value);
                    if (tab5_dgv1.Rows[i].Cells["淘汰率"].Value != DBNull.Value && tab5_dgv1.Rows[i].Cells["淘汰率"].Value.ToString() == "-1")
                    {
                        tab5_dgv1.Rows[i].Cells["淘汰率"].Value = "";
                    }
                    if (tab5_dgv1.Rows[i].Cells["筛选数量"].Value != DBNull.Value && tab5_dgv1.Rows[i].Cells["筛选数量"].Value.ToString() == "-1")
                    {
                        tab5_dgv1.Rows[i].Cells["筛选数量"].Value = "";
                    }
                }
                Tools.CloseConnection(tmpconn);
            }
            tab5_dgv1.AutoResizeColumns();
        }

        private void tab5_btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.xls)|*.xls";
            sfd.ShowDialog();
            if (sfd.FileName.Length > 0)
            {
                if (tab5_dgv1.Rows.Count > 0)
                {
                    //if (tab5_ExportToExcel(sfd.FileName, tab5_dgv1.Rows))
                    //ExcelHelper.DataToExcel eh = new ExcelHelper.DataToExcel();
                    //if (eh.ExportToExcel ((DataTable )tab5_dgv1.DataSource  ,sfd .FileName ,true,"sheet1"))
                    if (ExcelHelper.OutputToExcel ((DataTable )tab5_dgv1.DataSource ,sfd.FileName))
                    {
                        MessageBox.Show("Excel文件导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //ShowErrMsg("Excel文件导出失败！");
                        ShowErrMsg("Excel文件导出失败！");
                    }
                }
                else
                {
                    MessageBox.Show("无记录可以导出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private bool tab5_ExportToExcel(string strTmpFileName, DataGridViewRowCollection tmpdgvrc)
        {
            if (tmpdgvrc.Count == 0)
            {
                return false;
            }
            try
            {
                Excel.Application tmpapp = new Excel.Application();
                Excel.Workbook tmpwb = tmpapp.Workbooks.Add(true);
                Excel.Worksheet tmpws = (Excel.Worksheet)tmpwb.Worksheets[1];

                tmpws.Name = "下厂验收清单";
                Excel.Range tmprange;
                //标题
                tmprange = tmpws.get_Range("A1", "K1");
                tmprange.Merge(0);
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Name = "黑体";
                tmprange.Font.Size = 15;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "下厂验收清单";
                //列头
                tmprange = (Excel.Range)tmpws.Cells[2, 1];
                tmprange.ColumnWidth = 15;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "备资料日期";
                tmprange = (Excel.Range)tmpws.Cells[2, 2];
                tmprange.ColumnWidth = 15;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "合同号";
                tmprange = (Excel.Range)tmpws.Cells[2, 3];
                tmprange.ColumnWidth = 35;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "型号规格";
                tmprange = (Excel.Range)tmpws.Cells[2, 4];
                tmprange.ColumnWidth = 30;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "执行标准";
                tmprange = (Excel.Range)tmpws.Cells[2, 5];
                tmprange.ColumnWidth = 12;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "生产批号";
                tmprange = (Excel.Range)tmpws.Cells[2, 6];
                tmprange.ColumnWidth = 11;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "合同数量";
                tmprange = (Excel.Range)tmpws.Cells[2, 7];
                tmprange.ColumnWidth = 11;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "提交数量";
                tmprange = (Excel.Range)tmpws.Cells[2, 8];
                tmprange.ColumnWidth = 11;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "筛选数量";
                tmprange = (Excel.Range)tmpws.Cells[2, 9];
                tmprange.ColumnWidth = 11;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "淘汰率";
                tmprange = (Excel.Range)tmpws.Cells[2, 10];
                tmprange.ColumnWidth = 15;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "生产日期";
                tmprange = (Excel.Range)tmpws.Cells[2,11];
                tmprange.ColumnWidth = 18;
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Font.Bold = true;
                tmprange.Value2 = "验收单位";
                //内容
                for (int i = tmpdgvrc.Count - 1; i >= 0; i--)
                {
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 1];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = Convert.ToDateTime ( tmpdgvrc[i].Cells["备资料日期"].Value).ToString ("yyyy-MM-dd");
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 2];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpdgvrc[i].Cells["合同号"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 3];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlLeft;
                    tmprange.Value2 = tmpdgvrc[i].Cells["型号规格"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 4];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlLeft;
                    tmprange.Value2 = tmpdgvrc[i].Cells["执行标准"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 5];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpdgvrc[i].Cells["生产批号"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 6];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlRight;
                    tmprange.Value2 = tmpdgvrc[i].Cells["合同数量"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 7];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlRight;
                    tmprange.Value2 = tmpdgvrc[i].Cells["提交数量"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 8];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlRight;
                    tmprange.Value2 = tmpdgvrc[i].Cells["筛选数量"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 9];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpdgvrc[i].Cells["淘汰率"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 10];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpdgvrc[i].Cells["生产日期"].Value;
                    tmprange = (Excel.Range)tmpws.Cells[2 + tmpdgvrc.Count - i, 11];
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlLeft;
                    tmprange.Value2 = tmpdgvrc[i].Cells["验收单位"].Value;
                }
                tmpwb.SaveAs(strTmpFileName, Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                tmpapp.Quit();
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 产品库存清单
        private void tab6_btnSearch_Click(object sender, EventArgs e)
        {
            string strSelectString = tab2_DefualtSelectString;
            string strModelFilter = string.Empty;
            //处理质量等级筛选
            if (tab2_clbLevel.CheckedItems.Count > 0)
            {
                if (tab2_clbLevel.CheckedItems.Count > 1)
                {
                    strModelFilter += "(";
                }
                for (int i = 0; i < tab2_clbLevel.CheckedItems.Count; i++)
                {
                    if (i > 0)
                    {
                        strModelFilter += " or ";
                    }
                    switch (tab2_clbLevel.CheckedItems[i].ToString())
                    {
                        case "七专":
                            strModelFilter += "(htb003 not like 'CC-%' and htb003 not like 'J-%' and htb003 not like 'M-%' and htb003 not like 'GJB-%')";
                            break;
                        case "CAST":
                            strModelFilter += "(htb003 like 'CC-%')";
                            break;
                        case "普军":
                            strModelFilter += "(htb003 like 'J-%')";
                            break;
                        case "工业级":
                            strModelFilter += "(htb003 like 'M-%')";
                            break;
                        case "国军标":
                            strModelFilter += "(htb003 like 'GJB-%')";
                            break;
                    }
                }
                if (tab2_clbLevel.CheckedItems.Count > 1)
                {
                    strModelFilter += ")";
                }
                strSelectString += " and " + strModelFilter;
            }
            //处理交货期范围筛选
            strSelectString += " and (htb010 between '" + tab2_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab2_dtpTo.Value.ToString("yyyyMMdd") + "')";
            //处理执行标准和补充协议筛选、合同备注或型号规格
            //if (tab2_txtSearchKey.Text.Trim().Length > 0)
            //{
            //    switch (tab2_CombSearchField.SelectedIndex)
            //    {
            //        case 0:
            //            strSelectString += " and (htb021 like '" + tab2_txtSearchKey.Text.Trim() + "' or htb022 like '" + tab2_txtSearchKey.Text.Trim() + "')";
            //            break;
            //        case 1:
            //            strSelectString += " and htc002 like '" + tab2_txtSearchKey.Text.Trim() + "'";
            //            break;
            //        case 2:
            //            strSelectString += " and htb003 like '" + tab2_txtSearchKey.Text.Trim() + "'";
            //            break;
            //        case 3:
            //            strSelectString += " and hta011 like '" + tab2_txtSearchKey.Text.Trim() + "'";
            //            break;
            //    }
            //}
            //排序
            string strSortBy = string.Empty;
            switch (tab2_cbbSortBy.Text)
            {
                case "合同号":
                    strSortBy = "hta011";
                    break;
                case "型号规格":
                    strSortBy = "htb003";
                    break;
                case "交货期":
                    strSortBy = "htb010";
                    break;
                case "合同数量":
                    strSortBy = "htb006";
                    break;
                case "已出库数量":
                    strSortBy = "htb018";
                    break;
            }
            string strSortType = string.Empty;
            switch (tab2_cbbSortType.Text)
            {
                case "升序":
                    strSortType = "ASC";
                    break;
                case "降序":
                    strSortType = "DESC";
                    break;
            }
            strSelectString += " order by " + strSortBy + " " + strSortType;
            DataTable dt = new DataTable();
            dt.Columns.Add("合同号");
            dt.Columns.Add("型号规格");
            dt.Columns.Add("合同数量", typeof(int));
            dt.Columns.Add("交货期");
            dt.Columns.Add("已出库数量", typeof(int));
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpda = new SqlDataAdapter(strSelectString, conn);
            Tools.OpenConnection(conn);
            tmpda.Fill(dt);
            Tools.CloseConnection(conn);
            tab2_dgv1.DataSource = dt;
            tab2_tbTotalRecord.Text = dt.Rows.Count.ToString();
            tab2_dgv1.AutoResizeColumns();
        }

        private void tab6_btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd=new SaveFileDialog ();
            sfd.Filter ="*.xls";
            sfd.ShowDialog ();
            if (sfd.FileName != null)
            {
                //ExcelHelper.DataToExcel eh = new ExcelHelper.DataToExcel();
                //if (eh.ExportToExcel((DataTable)tab6_dgv1 .DataSource  , sfd.FileName, true, "产品库存清单" ))
                if (ExcelHelper .OutputToExcel ((DataTable)tab6_dgv1 .DataSource  , sfd.FileName))
                {
                    MessageBox.Show("产品库存清单导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //MessageBox.Show("产品库存清单导出失败！"+eh.strErrMsg , "提示", MessageBoxButtons.OK, MessageBoxIcon.Error );
                    MessageBox.Show("产品库存清单导出失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        #endregion

        #region 合同履约率报表
        private void tab7_rb1_CheckedChanged(object sender, EventArgs e)
        {
            if (tab7_rb1.Checked)
            {
                tab7_rb2.Checked = false;
            }
        }

        private void tab7_rb2_CheckedChanged(object sender, EventArgs e)
        {
            if (tab7_rb2.Checked)
            {
                tab7_rb1.Checked = false;
            }
        }


        private void tab7_btnQuery_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = tab7_SelectAllContractCount;
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlCommand tmpcmd;
            if (tab7_rb1.Checked)
            {
                strTmpSelectString +=" and htb010 between '"+tab7_dp1 .Value .ToString ("yyyyMMdd")+"' and '"+tab7_dp2 .Value .ToString ("yyyyMMdd")+"'";
            }
            else
            {
                strTmpSelectString += " and left(htb001,8) between '" + tab7_dp1.Value.ToString("yyyyMMdd") + "' and '" + tab7_dp2.Value.ToString("yyyyMMdd") + "'";
            }
            tmpcmd =new SqlCommand (strTmpSelectString ,conn);
            tmpcmd.CommandTimeout = 0;
            Tools.OpenConnection(conn);
            tab7_txt1.Text = GetInt(tmpcmd.ExecuteScalar()).ToString();
            tmpcmd.CommandText = strTmpSelectString + " and (select sum(gxck015-gxck019) from gxck_table where (gxck016='Y' or gxck016='K') and gxck010=htb001 and gxck011=htb002 and gxck002<=htb010)<htb006";
            tab7_txt2.Text =Convert.ToString (GetInt (tab7_txt1 .Text )- GetInt(tmpcmd.ExecuteScalar()));
            tmpcmd.CommandText = strTmpSelectString + " and (select sum(xhb008) from orxhb_table,gxck_table where xhb010='Y' and left(xhb014,12)=gxck001 and gxck010=htb001 and gxck011=htb002 and left(xhb001,8)<=htb010)<htb006";
            tab7_txt4.Text = Convert.ToString(GetInt(tab7_txt1.Text) - GetInt(tmpcmd.ExecuteScalar()));
            if (tab7_txt1.Text == "0")
            {
                tab7_txt3.Text = "0.00";
                tab7_txt5.Text = "0.00";
            }
            else
            {
                tab7_txt3.Text = Convert.ToDouble((GetInt(tab7_txt2.Text) * 100) / GetInt(tab7_txt1.Text)).ToString("#.##");
                tab7_txt5.Text = Convert.ToDouble((GetInt(tab7_txt4.Text) * 100) / GetInt(tab7_txt1.Text)).ToString("#.##");
            }
            Tools.CloseConnection(conn);
        }
        #endregion

        #region 市场部统计报表
        
        private void tab8_rb1_CheckedChanged(object sender, EventArgs e)
        {
            if (tab8_rb1.Checked)
            {
                tab8_cmbYear.Enabled = true;
                tab8_cmbMonth.Enabled = true;
                tab8_rb2.Checked = false;
                tab8_dtp1.Enabled = false;
                tab8_dtp2.Enabled = false;
            }
        }

        private void tab8_rb2_CheckedChanged(object sender, EventArgs e)
        {
            if (tab8_rb2.Checked)
            {
                tab8_rb1.Checked = false;
                tab8_dtp1.Enabled = true;
                tab8_dtp2.Enabled = true;
                tab8_cmbYear.Enabled = false;
                tab8_cmbMonth.Enabled = false;
            }
        }

        private void tab8_btnSearch1_Click(object sender, EventArgs e)
        {
            DataTable tmpdt = new DataTable();
            tmpdt.Columns.Add("片区");
            tmpdt.Columns.Add("订货数量");
            tmpdt.Columns.Add("订货金额");
            tmpdt.Columns.Add("销货数量");
            tmpdt.Columns.Add("销货金额");
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            string strTmpSelectString=string.Empty ;
            if (tab8_rb1.Checked)
            {
                strTmpSelectString = tab8_SelectOrderSumary.Replace("@年月", tab8_cmbYear.Text + tab8_cmbMonth.Text);
            }
            else
            {
                strTmpSelectString = tab8_SelectOrderSumaryDateArea.Replace("@从", tab8_dtp1.Value.ToString("yyyyMMdd"))
                                                                                                                    .Replace("@到", tab8_dtp2.Value.ToString("yyyyMMdd"));
            }
            SqlDataAdapter tmpda=new SqlDataAdapter (strTmpSelectString ,conn);
            Tools.OpenConnection(conn);
            tmpda.Fill(tmpdt);
            if (tmpdt.Rows.Count > 0)
            {
                DataTable tmpdt2;
                string strTmpSelectString2 = string.Empty;
                for (int i = 0; i < tmpdt.Rows.Count;i++)
                {
                    tmpdt2 = new DataTable();
                    if (tab8_rb1.Checked)
                    {
                        strTmpSelectString2 = tab8_SelectXHSumary.Replace("@年月", tab8_cmbYear.Text + tab8_cmbMonth.Text)
                                                                                                      .Replace("@片区", GetString(tmpdt.Rows[i]["片区"]));
                    }
                    else
                    {
                        strTmpSelectString2 = tab8_SelectXHSumaryDateArea.Replace("@从", tab8_dtp1.Value.ToString("yyyyMMdd"))
                                                                                                                    .Replace("@到", tab8_dtp2.Value.ToString("yyyyMMdd"))
                                                                                                                    .Replace("@片区", GetString(tmpdt.Rows[i]["片区"]));
                    }
                    tmpda.SelectCommand.CommandText = strTmpSelectString2;
                    tmpda.Fill(tmpdt2);
                    if (tmpdt2.Rows.Count > 0)
                    {
                        tmpdt.Rows[i]["销货数量"] = tmpdt2.Rows[0]["销货数量"];
                        tmpdt.Rows[i]["销货金额"] = tmpdt2.Rows[0]["销货金额"];
                    }
                }
            }
            Tools.CloseConnection(conn);
            tab8_dgv1.DataSource = tmpdt;
            tab8_dgv1.AutoResizeColumns();
        }

        #endregion

        #region 每月欠交汇总
        private void tab9_btnSearch_Click(object sender, EventArgs e)
        {
            if (tab9_cmbYear.Text == "" || tab9_cmbMonth.Text == "")
            {
                MessageBox.Show("请先选择要查询的年月！");
                tab9_cmbYear.Focus();
                return;
            }
            tab9_lblQSL.Text = tab9_cmbMonth.Text + "月前欠交数量";
            tab9_lblQXS.Text = tab9_cmbMonth.Text + "月前欠交项数";
            tab9_lblSL.Text = tab9_cmbMonth.Text + "月欠交数量";
            tab9_lblXS.Text = tab9_cmbMonth.Text + "月欠交项数";
            tab9_lblHSL.Text = tab9_cmbMonth.Text + "月后欠交数量";
            tab9_lblHXS.Text = tab9_cmbMonth.Text + "月后欠交项数";
            int iLastYear =GetInt ( tab9_cmbYear.Text);
            int iLastMonth=GetInt ( tab9_cmbMonth .Text )-1;
            if (tab9_cmbMonth.Text == "1")
            {
                iLastMonth = 12;
                iLastYear = GetInt(tab9_cmbYear.Text) - 1;
            }
            int iCurYear = GetInt(tab9_cmbYear.Text);
            int iCurMonth = GetInt(tab9_cmbMonth.Text);
            int iNextYear = GetInt(tab9_cmbYear.Text);
            int iNextMonth = GetInt(tab9_cmbMonth.Text)+1;
            if (tab9_cmbMonth .Text =="12")
            {
                iNextMonth = 1;
                iNextYear = GetInt(tab9_cmbYear.Text) + 1;
            }
            string strQDateFrom = "20100101";
            string strQDateTo=iLastYear .ToString ("0000") +iLastMonth .ToString ("00")+DateTime.DaysInMonth (iLastYear,iLastMonth).ToString ("00");
            string strDateFrom = iCurYear.ToString("0000") + iCurMonth.ToString("00") + "01";
            string strDateTo = iCurYear.ToString("0000") + iCurMonth.ToString("00") + DateTime.DaysInMonth(iCurYear, iCurMonth).ToString("00");
            string strHDate = iNextYear.ToString("0000") + iNextMonth.ToString("00")+"01";
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            Tools.OpenConnection (conn);
            SqlCommand tmpcmd;
            //CC
            tmpcmd = new SqlCommand(tab9_SelectCCZSL, conn);
            tab9_txtCCZSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCZXC, conn);
            tab9_txtCCZXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCQSL.Replace ("@QDateFrom",strQDateFrom).Replace ("@QDateTo",strQDateTo ), conn);
            tab9_txtCCQSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCQXC.Replace("@QDateFrom", strQDateFrom).Replace("@QDateTo", strQDateTo), conn);
            tab9_txtCCQXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCSL.Replace ("@DateFrom",strDateFrom ).Replace ("@DateTo",strDateTo ), conn);
            tab9_txtCCSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCXC.Replace("@DateFrom", strDateFrom).Replace("@DateTo", strDateTo), conn);
            tab9_txtCCXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCHSL.Replace ("@HDate",strHDate ), conn);
            tab9_txtCCHSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectCCHXC.Replace("@HDate", strHDate), conn);
            tab9_txtCCHXS.Text = GetString(tmpcmd.ExecuteScalar());
            //一般七专
            tmpcmd = new SqlCommand(tab9_SelectGZSL, conn);
            tab9_txtGZSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGZXC, conn);
            tab9_txtGZXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGQSL.Replace("@QDateFrom", strQDateFrom).Replace("@QDateTo", strQDateTo), conn);
            tab9_txtGQSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGQXC.Replace("@QDateFrom", strQDateFrom).Replace("@QDateTo", strQDateTo), conn);
            tab9_txtGQXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGSL.Replace("@DateFrom", strDateFrom).Replace("@DateTo", strDateTo), conn);
            tab9_txtGSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGXC.Replace("@DateFrom", strDateFrom).Replace("@DateTo", strDateTo), conn);
            tab9_txtGXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGHSL.Replace("@HDate", strHDate), conn);
            tab9_txtGHSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectGHXC.Replace("@HDate", strHDate), conn);
            tab9_txtGHXS.Text = GetString(tmpcmd.ExecuteScalar());
            //普军
            tmpcmd = new SqlCommand(tab9_SelectJZSL, conn);
            tab9_txtJZSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJZXC, conn);
            tab9_txtJZXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJQSL.Replace("@QDateFrom", strQDateFrom).Replace("@QDateTo", strQDateTo), conn);
            tab9_txtJQSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJQXC.Replace("@QDateFrom", strQDateFrom).Replace("@QDateTo", strQDateTo), conn);
            tab9_txtJQXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJSL.Replace("@DateFrom", strDateFrom).Replace("@DateTo", strDateTo), conn);
            tab9_txtJSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJXC.Replace("@DateFrom", strDateFrom).Replace("@DateTo", strDateTo), conn);
            tab9_txtJXS.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJHSL.Replace("@HDate", strHDate), conn);
            tab9_txtJHSL.Text = GetString(tmpcmd.ExecuteScalar());
            tmpcmd = new SqlCommand(tab9_SelectJHXC.Replace("@HDate", strHDate), conn);
            tab9_txtJHXS.Text = GetString(tmpcmd.ExecuteScalar());
            Tools.CloseConnection(conn);
        }
        #endregion

        #region 年度产品出入库及提交汇总
        private void tab10_txtYear_Leave(object sender, EventArgs e)
        {
            if (tab10_txtYear.Text.Trim ().Length > 4)
            {
                tab10_txtYear.Text = tab10_txtYear.Text.Trim().Substring(0, 4); 
            }
            tab10_txtYear .Text = GetInt(tab10_txtYear.Text).ToString("0000");
            if (tab10_txtYear.Text == "0000")
            {
                tab10_txtYear.Clear();
            }
        }

        private void tab10_btnQuery_Click(object sender, EventArgs e)
        {
            if (tab10_txtYear.Text.Length == 0)
            {
                MessageBox.Show("请先填入四位数年份", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning );
                tab10_txtYear.Focus();
                return;
            }
            string strYear = tab10_txtYear.Text;
            double drk;
            double drkwg;
            double drkwgbfb;
            double dck;
            double dckwg;
            double dckwgbfb;
            double dtj;
            double dtjwg;
            double dtjwgbfb;
            double dtjzt;
            double dtjztbfb;
            double dtjztcur;
            double dtjztcurbfb;
            double dtjztkc;
            double dtjztkcbfb;
            SqlCommand cmd=new SqlCommand ();
            SqlConnection conn_tp = Tools.GetConnection(strConnectionStringTP);
            cmd.Connection =conn_tp ;
            cmd .CommandType =CommandType .Text ;
            Tools.OpenConnection(conn_tp);
            #region 七专库
            //入库数量
            cmd.CommandText = tab10_SelectQzrksl.Replace("@Year", strYear);
            drk =GetDouble (cmd.ExecuteScalar());
            tab10_tv1 .Nodes ["qz"].Nodes["rksl"].Text ="入库数量("+drk.ToString()+")";
            //入库外购数量
            cmd.CommandText = tab10_SelectQzrkwgsl.Replace("@Year", strYear);
            drkwg =GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["qz"].Nodes["rkwg"].Text = "入库外购数量(" + drkwg.ToString() + ")";
            //入库外购百分比
            if (drk >0)
            {
                drkwgbfb =(drkwg /drk )*100;
            }
            else 
            {
                drkwgbfb =0d;
            }
            tab10_tv1.Nodes["qz"].Nodes["rkwgbfb"].Text = "入库外购百分比(" + drkwgbfb.ToString("0.00") + ")";
            //出库数量
            cmd.CommandText = tab10_SelectQzcksl.Replace("@Year", strYear);
            dck = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["qz"].Nodes["cksl"].Text = "出库数量(" + dck.ToString() + ")";
            //出库外购数量
            cmd.CommandText = tab10_SelectQzckwgsl.Replace("@Year", strYear);
            dckwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["qz"].Nodes["ckwg"].Text = "出库外购数量(" + dckwg.ToString() + ")";
            //出库外购百分比
            if (dck > 0)
            {
                dckwgbfb = (dckwg / dck) * 100;
            }
            else
            {
                dckwgbfb = 0d;
            }
            tab10_tv1.Nodes["qz"].Nodes["ckwgbfb"].Text = "出库外购百分比(" + dckwgbfb.ToString("0.00") + ")";
            #endregion
            #region 一般品库
            //入库数量
            cmd.CommandText = tab10_SelectYbprksl.Replace("@Year", strYear);
            drk = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["ybp"].Nodes["rksl"].Text = "入库数量(" + drk.ToString() + ")";
            //入库外购数量
            cmd.CommandText = tab10_SelectYbprkwgsl.Replace("@Year", strYear);
            drkwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["ybp"].Nodes["rkwg"].Text = "入库外购数量(" + drkwg.ToString() + ")";
            //入库外购百分比
            if (drk > 0)
            {
                drkwgbfb = (drkwg / drk) * 100;
            }
            else
            {
                drkwgbfb = 0d;
            }
            tab10_tv1.Nodes["ybp"].Nodes["rkwgbfb"].Text = "入库外购百分比(" + drkwgbfb.ToString("0.00") + ")";
            //出库数量
            cmd.CommandText = tab10_SelectYbpcksl.Replace("@Year", strYear);
            dck = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["ybp"].Nodes["cksl"].Text = "出库数量(" + dck.ToString() + ")";
            //出库外购数量
            cmd.CommandText = tab10_SelectYbpckwgsl.Replace("@Year", strYear);
            dckwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["ybp"].Nodes["ckwg"].Text = "出库外购数量(" + dckwg.ToString() + ")";
            //出库外购百分比
            if (dck > 0)
            {
                dckwgbfb = (dckwg / dck) * 100;
            }
            else
            {
                dckwgbfb = 0d;
            }
            tab10_tv1.Nodes["ybp"].Nodes["ckwgbfb"].Text = "出库外购百分比(" + dckwgbfb.ToString("0.00") + ")";
            #endregion
            #region 半成品库
            string strSYear = GetSYear(strYear);
            string strNYear = strYear.Substring (2,2);
            //入库数量
            cmd.CommandText = tab10_SelectBcprksl.Replace("@Year", strYear).Replace("@SYear", strSYear).Replace("@NYear", strNYear);
            drk = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["bcp"].Nodes["rksl"].Text = "入库数量(" + drk.ToString() + ")";
            //入库外购数量
            cmd.CommandText = tab10_SelectBcprkwgsl.Replace("@Year", strYear).Replace("@SYear", strSYear).Replace("@NYear", strNYear);
            drkwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["bcp"].Nodes["rkwg"].Text = "入库外购数量(" + drkwg.ToString() + ")";
            //入库外购百分比
            if (drk > 0)
            {
                drkwgbfb = (drkwg / drk) * 100;
            }
            else
            {
                drkwgbfb = 0d;
            }
            tab10_tv1.Nodes["bcp"].Nodes["rkwgbfb"].Text = "入库外购百分比(" + drkwgbfb.ToString("0.00") + ")";
            //出库数量
            cmd.CommandText = tab10_SelectBcpcksl.Replace("@Year", strYear);
            dck = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["bcp"].Nodes["cksl"].Text = "出库数量(" + dck.ToString() + ")";
            //出库外购数量
            cmd.CommandText = tab10_SelectBcpckwgsl.Replace("@Year", strYear);
            dckwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv1.Nodes["bcp"].Nodes["ckwg"].Text = "出库外购数量(" + dckwg.ToString() + ")";
            //出库外购百分比
            if (dck > 0)
            {
                dckwgbfb = (dckwg / dck) * 100;
            }
            else
            {
                dckwgbfb = 0d;
            }
            tab10_tv1.Nodes["bcp"].Nodes["ckwgbfb"].Text = "出库外购百分比(" + dckwgbfb.ToString("0.00") + ")";
            #endregion
            tab10_tv1.Refresh();
            tab10_tv1.ExpandAll();
            Tools.CloseConnection (conn_tp );
            cmd = new SqlCommand();
            SqlConnection conn_db = Tools.GetConnection(strConnectionStringDB);
            cmd.Connection = conn_db;
            cmd.CommandType = CommandType.Text;
            Tools.OpenConnection(conn_db);
            cmd.Connection = conn_db;
            #region 提交
            #region 一般七专
            //提交总数
            cmd.CommandText = tab10_SelectTJ_G.Replace("@Year", strYear);
            dtj = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["G"].Nodes["tjzs"].Text = "提交总数(" + dtj.ToString() + ")";
            //外购数量
            cmd.CommandText = tab10_SelectTJ_G_wg.Replace("@Year", strYear);
            dtjwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["G"].Nodes["wgsl"].Text = "外购数量(" + dtjwg.ToString() + ")";
            //外购百分比
            if (dtj > 0)
            {
                dtjwgbfb = (dtjwg / dtj) * 100;
            }
            else
            {
                dtjwgbfb = 0d;
            }
            tab10_tv2.Nodes["G"].Nodes["wgbfb"].Text = "外购百分比(" + dtjwgbfb.ToString("0.00") + ")";
            //自投数量
            dtjzt = dtj - dtjwg;
            tab10_tv2.Nodes["G"].Nodes["ztsl"].Text = "自投数量(" + dtjzt.ToString() + ")";
            //自投百分比
            dtjztbfb = 100 - dtjwgbfb;
            tab10_tv2.Nodes["G"].Nodes["ztbfb"].Text = "自投百分比(" + dtjztbfb.ToString("0.00") + ")";
            //当年投产数量
            cmd.CommandText = tab10_SelectTJ_G_zz_cur.Replace("@Year", strYear).Replace ("@SYear",strSYear ).Replace ("@NYear",strNYear );
            dtjztcur = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["G"].Nodes["ztsl"].Nodes["dntc"].Text = strNYear + "年投产数量(" + dtjztcur.ToString() + ")";
            //当年投产百分比
            if (dtjzt > 0)
            {
                dtjztcurbfb = (dtjztcur / dtjzt) * 100;
            }
            else
            {
                dtjztcurbfb = 0d;
            }
            tab10_tv2.Nodes["G"].Nodes["ztsl"].Nodes["dnbfb"].Text = strNYear + "年投产百分比(" + dtjztcurbfb.ToString() + ")";
            //库存数量
            dtjztkc = dtjzt - dtjztcur;
            tab10_tv2.Nodes["G"].Nodes["ztsl"].Nodes["kcsl"].Text = strNYear + "年以前的库存产品(" + dtjztkc.ToString() + ")";
            //库存百分比
            dtjztkcbfb = 100 - dtjztcurbfb;
            tab10_tv2.Nodes["G"].Nodes["ztsl"].Nodes["kcbfb"].Text = strNYear + "年以前的库存产品百分比(" + dtjztkcbfb.ToString() + ")";
            #endregion
            #region CC
            //提交总数
            cmd.CommandText = tab10_SelectTJ_CC.Replace("@Year", strYear);
            dtj = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["CC"].Nodes["tjzs"].Text = "提交总数(" + dtj.ToString() + ")";
            //外购数量
            cmd.CommandText = tab10_SelectTJ_CC_wg.Replace("@Year", strYear);
            dtjwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["CC"].Nodes["wgsl"].Text = "外购数量(" + dtjwg.ToString() + ")";
            //外购百分比
            if (dtj > 0)
            {
                dtjwgbfb = (dtjwg / dtj) * 100;
            }
            else
            {
                dtjwgbfb = 0d;
            }
            tab10_tv2.Nodes["CC"].Nodes["wgbfb"].Text = "外购百分比(" + dtjwgbfb.ToString("0.00") + ")";
            //自投数量
            dtjzt = dtj - dtjwg;
            tab10_tv2.Nodes["CC"].Nodes["ztsl"].Text = "自投数量(" + dtjzt.ToString() + ")";
            //自投百分比
            dtjztbfb = 100 - dtjwgbfb;
            tab10_tv2.Nodes["CC"].Nodes["ztbfb"].Text = "自投百分比(" + dtjztbfb.ToString("0.00") + ")";
            //当年投产数量
            cmd.CommandText = tab10_SelectTJ_CC_cur.Replace("@Year", strYear).Replace("@SYear", strSYear).Replace("@NYear", strNYear);
            dtjztcur = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["CC"].Nodes["ztsl"].Nodes["dntc"].Text = strNYear + "年投产数量(" + dtjztcur.ToString() + ")";
            //当年投产百分比
            if (dtjzt > 0)
            {
                dtjztcurbfb = (dtjztcur / dtjzt) * 100;
            }
            else
            {
                dtjztcurbfb = 0d;
            }
            tab10_tv2.Nodes["CC"].Nodes["ztsl"].Nodes["dnbfb"].Text = strNYear + "年投产百分比(" + dtjztcurbfb.ToString() + ")";
            //库存数量
            dtjztkc = dtjzt - dtjztcur;
            tab10_tv2.Nodes["CC"].Nodes["ztsl"].Nodes["kcsl"].Text = strNYear + "年以前的库存产品(" + dtjztkc.ToString() + ")";
            //库存百分比
            dtjztkcbfb = 100 - dtjztcurbfb;
            tab10_tv2.Nodes["CC"].Nodes["ztsl"].Nodes["kcbfb"].Text = strNYear + "年以前的库存产品百分比(" + dtjztkcbfb.ToString() + ")";
            #endregion
            #region 普军
            //提交总数
            cmd.CommandText = tab10_SelectTJ_J.Replace("@Year", strYear);
            dtj = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["J"].Nodes["tjzs"].Text = "提交总数(" + dtj.ToString() + ")";
            //外购数量
            cmd.CommandText = tab10_SelectTJ_J_wg.Replace("@Year", strYear);
            dtjwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["J"].Nodes["wgsl"].Text = "外购数量(" + dtjwg.ToString() + ")";
            //外购百分比
            if (dtj > 0)
            {
                dtjwgbfb = (dtjwg / dtj) * 100;
            }
            else
            {
                dtjwgbfb = 0d;
            }
            tab10_tv2.Nodes["J"].Nodes["wgbfb"].Text = "外购百分比(" + dtjwgbfb.ToString("0.00") + ")";
            //自投数量
            dtjzt = dtj - dtjwg;
            tab10_tv2.Nodes["J"].Nodes["ztsl"].Text = "自投数量(" + dtjzt.ToString() + ")";
            //自投百分比
            dtjztbfb = 100 - dtjwgbfb;
            tab10_tv2.Nodes["J"].Nodes["ztbfb"].Text = "自投百分比(" + dtjztbfb.ToString("0.00") + ")";
            //当年投产数量
            cmd.CommandText = tab10_SelectTJ_J_cur .Replace("@Year", strYear).Replace("@SYear", strSYear).Replace("@NYear", strNYear);
            dtjztcur = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["J"].Nodes["ztsl"].Nodes["dntc"].Text = strNYear + "年投产数量(" + dtjztcur.ToString() + ")";
            //当年投产百分比
            if (dtjzt > 0)
            {
                dtjztcurbfb = (dtjztcur / dtjzt) * 100;
            }
            else
            {
                dtjztcurbfb = 0d;
            }
            tab10_tv2.Nodes["J"].Nodes["ztsl"].Nodes["dnbfb"].Text = strNYear + "年投产百分比(" + dtjztcurbfb.ToString() + ")";
            //库存数量
            dtjztkc = dtjzt - dtjztcur;
            tab10_tv2.Nodes["J"].Nodes["ztsl"].Nodes["kcsl"].Text = strNYear + "年以前的库存产品(" + dtjztkc.ToString() + ")";
            //库存百分比
            dtjztkcbfb = 100 - dtjztcurbfb;
            tab10_tv2.Nodes["J"].Nodes["ztsl"].Nodes["kcbfb"].Text = strNYear + "年以前的库存产品百分比(" + dtjztkcbfb.ToString() + ")";
            #endregion
            #region 工业级
            //提交总数
            cmd.CommandText = tab10_SelectTJ_M.Replace("@Year", strYear);
            dtj = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["M"].Nodes["tjzs"].Text = "提交总数(" + dtj.ToString() + ")";
            //外购数量
            cmd.CommandText = tab10_SelectTJ_M_wg.Replace("@Year", strYear);
            dtjwg = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["M"].Nodes["wgsl"].Text = "外购数量(" + dtjwg.ToString() + ")";
            //外购百分比
            if (dtj > 0)
            {
                dtjwgbfb = (dtjwg / dtj) * 100;
            }
            else
            {
                dtjwgbfb = 0d;
            }
            tab10_tv2.Nodes["M"].Nodes["wgbfb"].Text = "外购百分比(" + dtjwgbfb.ToString("0.00") + ")";
            //自投数量
            dtjzt = dtj - dtjwg;
            tab10_tv2.Nodes["M"].Nodes["ztsl"].Text = "自投数量(" + dtjzt.ToString() + ")";
            //自投百分比
            dtjztbfb = 100 - dtjwgbfb;
            tab10_tv2.Nodes["M"].Nodes["ztbfb"].Text = "自投百分比(" + dtjztbfb.ToString("0.00") + ")";
            //当年投产数量
            cmd.CommandText = tab10_SelectTJ_M_cur.Replace("@Year", strYear).Replace("@SYear", strSYear).Replace("@NYear", strNYear);
            dtjztcur = GetDouble(cmd.ExecuteScalar());
            tab10_tv2.Nodes["M"].Nodes["ztsl"].Nodes["dntc"].Text = strNYear + "年投产数量(" + dtjztcur.ToString() + ")";
            //当年投产百分比
            if (dtjzt > 0)
            {
                dtjztcurbfb = (dtjztcur / dtjzt) * 100;
            }
            else
            {
                dtjztcurbfb = 0d;
            }
            tab10_tv2.Nodes["M"].Nodes["ztsl"].Nodes["dnbfb"].Text = strNYear + "年投产百分比(" + dtjztcurbfb.ToString() + ")";
            //库存数量
            dtjztkc = dtjzt - dtjztcur;
            tab10_tv2.Nodes["M"].Nodes["ztsl"].Nodes["kcsl"].Text = strNYear + "年以前的库存产品(" + dtjztkc.ToString() + ")";
            //库存百分比
            dtjztkcbfb = 100 - dtjztcurbfb;
            tab10_tv2.Nodes["M"].Nodes["ztsl"].Nodes["kcbfb"].Text = strNYear + "年以前的库存产品百分比(" + dtjztkcbfb.ToString() + ")";
            #endregion
            Tools.CloseConnection(conn_db);
            tab10_tv2.ExpandAll();
            tab10_tv2.Refresh();
            #endregion
        }

        private string GetSYear(string strYear)
        {
            string strTmp = strYear;
            if (strTmp.Length < 4)
            {
                strTmp = DateTime.Now.Year.ToString().Substring(2, 2);
            }
            if (strTmp.Length >= 4)
            {
                strTmp = strTmp.Substring(2, 2);
            }
            string strA = strTmp.Substring(0, 1);
            string strB = strTmp.Substring(1, 1);
            switch (strB)
            {
                case "0":
                    strB = "A";
                    break;
                case "1":
                    strB = "B";
                    break;
                case "2":
                    strB = "C";
                    break;
                case "3":
                    strB = "D";
                    break;
                case "4":
                    strB = "E";
                    break;
                case "5":
                    strB = "F";
                    break;
                case "6":
                    strB = "G";
                    break;
                case "7":
                    strB = "H";
                    break;
                case "8":
                    strB = "J";
                    break;
                case "9":
                    strB = "K";
                    break;
            }
            strTmp = strA + strB;
            return strTmp;
        }
        #endregion

        #region 生产计划统计汇总表
        private void tab11_btnQuery_Click(object sender, EventArgs e)
        {
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            string strFromDate = tab11_dtpFrom.Value.ToString("yyyyMMdd");
            string strToDate = tab11_dtpTo.Value.ToString("yyyyMMdd");
            SqlDataAdapter tmpsda = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            tmpsda.SelectCommand = cmd;
            Tools.OpenConnection(conn);
            //一般生产计划汇总
            DataTable tmpdt_yb_sum = new DataTable();
            tmpsda.SelectCommand.CommandText = tab11_SelectTotal_YB.Replace("@起始时间", strFromDate)
                                                                                                            .Replace("@截止时间", strToDate);
            tmpsda.Fill(tmpdt_yb_sum);
            tab11_txtTotalCount1.Clear();
            tab11_txtTotalQuantity1.Clear();
            if (tmpdt_yb_sum.Rows.Count > 0)
            {
                tab11_txtTotalCount1.Text = GetString(tmpdt_yb_sum.Rows[0]["总项数"]).Replace (".00","");
                tab11_txtTotalQuantity1.Text = GetString(tmpdt_yb_sum.Rows[0]["总数量"]).Replace(".00", "");
            }
            //一般生产计划明细
            DataTable tmpdt_yb_detail = new DataTable();
            tmpsda.SelectCommand.CommandText = tab11_SelectDetail_YB.Replace("@起始时间", strFromDate)
                                                                                                            .Replace("@截止时间", strToDate);
            tmpsda.Fill(tmpdt_yb_detail);
            tab11_dgv1.DataSource = tmpdt_yb_detail;
            //项目生产计划汇总
            DataTable tmpdt_xm_sum = new DataTable();
            tmpsda.SelectCommand.CommandText = tab11_SelectTotal_XM.Replace("@起始时间", strFromDate)
                                                                                                            .Replace("@截止时间", strToDate);
            tmpsda.Fill(tmpdt_xm_sum);
            tab11_txtTotalCount2.Clear();
            tab11_txtTotalQuantity2.Clear();
            if (tmpdt_xm_sum.Rows.Count > 0)
            {
                tab11_txtTotalCount2.Text = GetString(tmpdt_xm_sum.Rows[0]["总项数"]).Replace(".00", "");
                tab11_txtTotalQuantity2.Text = GetString(tmpdt_xm_sum.Rows[0]["总数量"]).Replace(".00", "");
            }
            //项目生产计划明细
            DataTable tmpdt_xm_detail = new DataTable();
            tmpsda.SelectCommand.CommandText = tab11_SelectDetail_XM.Replace("@起始时间", strFromDate)
                                                                                                            .Replace("@截止时间", strToDate);
            tmpsda.Fill(tmpdt_xm_detail);
            tab11_dgv2.DataSource = tmpdt_xm_detail;
            //补投汇总
            DataTable tmpdt_bt_sum = new DataTable();
            tmpsda.SelectCommand.CommandText = tab11_SelectTotal_BT.Replace("@起始时间", strFromDate)
                                                                                                            .Replace("@截止时间", strToDate);
            tmpsda.Fill(tmpdt_bt_sum);
            tab11_txtTotalCount3.Clear();
            tab11_txtTotalQuantity3.Clear();
            if (tmpdt_bt_sum.Rows.Count > 0)
            {
                tab11_txtTotalCount3.Text = GetString(tmpdt_bt_sum.Rows[0]["总批数"]).Replace(".00", "");
                tab11_txtTotalQuantity3.Text = GetString(tmpdt_bt_sum.Rows[0]["总数量"]).Replace(".00", "");
            }
            //项目生产计划明细
            DataTable tmpdt_bt_detail = new DataTable();
            tmpsda.SelectCommand.CommandText = tab11_SelectDetail_BT.Replace("@起始时间", strFromDate)
                                                                                                            .Replace("@截止时间", strToDate);
            tmpsda.Fill(tmpdt_bt_detail);
            tab11_dgv3.DataSource = tmpdt_bt_detail;
            Tools.CloseConnection(conn);
        }

        private void tab11_btnAutoResize1_Click(object sender, EventArgs e)
        {
            tab11_dgv1.AutoResizeColumns();
        }

        private void tab11_btnAutoResize2_Click(object sender, EventArgs e)
        {
            tab11_dgv2.AutoResizeColumns();
        }

        private void tab11_AutoResize3_Click(object sender, EventArgs e)
        {
            tab11_dgv3.AutoResizeColumns();
        }
        #endregion

        #region 移转单数差查询
        private void tab12_tmsiSelectAll_Click(object sender, EventArgs e)
        {
            for(int i=0;i<tab12_chklstGX .Items.Count ;i++)
            {
                tab12_chklstGX.SetItemChecked(i, true);
            }
        }

        private void tab12_tsmiSelectNone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tab12_chklstGX.Items.Count; i++)
            {
                tab12_chklstGX.SetItemChecked(i, false );
            }
        }

        private void tab12_btnClear_Click(object sender, EventArgs e)
        {
            tab12_txtSN.Clear();
            tab12_txtModel.Clear();
            tab12_tsmiSelectNone_Click(this, new EventArgs());
            tab12_dgv1.DataSource = null;
        }

        private void tab12_btnQuery_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = tab12_SelectGXYZSC;
            strTmpSelectString += " and gxyz012 between '" + tab12_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab12_dtpTo.Value.ToString("yyyyMMdd") + "'";
            if (tab12_txtSN.Text.Trim().Length > 0)
            {
                strTmpSelectString += " and gxyz027 like '" + tab12_txtSN.Text.Trim() + "'";
            }
            if (tab12_txtModel.Text.Trim().Length > 0)
            {
                strTmpSelectString += " and gxyz004 like '" + tab12_txtModel.Text.Trim() + "'";
            }
            if (tab12_chklstGX.CheckedItems.Count > 0)
            {
                string strTmpGX = string.Empty;
                for (int i = 0; i < tab12_chklstGX.CheckedItems.Count; i++)
                {
                    strTmpGX += "gxyz008='" + tab12_chklstGX.CheckedItems[i].ToString() + "'";
                    if (i < tab12_chklstGX.CheckedItems.Count-1)
                    {
                        strTmpGX += " or ";
                    }
                }
                strTmpSelectString += " and (" + strTmpGX + ")";
            }
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpsda = new SqlDataAdapter(strTmpSelectString, conn);
            DataTable tmpdt = new DataTable();
            Tools.OpenConnection(conn);
            tmpsda.Fill(tmpdt);
            Tools.CloseConnection(conn);
            tab12_dgv1.DataSource = tmpdt;
            tab12_dgv1.AutoResizeColumns();
        }

        private void tab12_btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.xls(*.xls)|*.xls";
            sfd.ShowDialog();
            if (sfd.FileName != null)
            {
                //ExcelHelper.DataToExcel eh = new ExcelHelper.DataToExcel();
                //if (eh.ExportToExcel((DataTable)tab12_dgv1.DataSource, sfd.FileName, true, "移转单数差清单"))
                if (ExcelHelper .OutputToExcel ((DataTable)tab12_dgv1.DataSource, sfd.FileName))
                {
                    MessageBox.Show("移转单数差清单导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //MessageBox.Show("移转单数差清单导出失败！" + eh.strErrMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    MessageBox.Show("移转单数差清单导出失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        #endregion

        #region 产品生产投试统计信息表
        private void tab13_btnQuery_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = tab13_SelectScjh;
            string strTmpSumString=tab13_SelectSumScjh ;
            strTmpSelectString += " and jha002 between '" + tab13_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab13_dtpTo.Value.ToString("yyyyMMdd") + "'";
            strTmpSumString += " and jha002 between '" + tab13_dtpFrom.Value.ToString("yyyyMMdd") + "' and '" + tab13_dtpTo.Value.ToString("yyyyMMdd") + "'";
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            Tools.OpenConnection(conn);
            //生产计划总项数,总数量
            SqlDataAdapter tmpsda1 = new SqlDataAdapter(strTmpSelectString, conn);
            SqlDataAdapter tmpsda2 = new SqlDataAdapter(strTmpSumString, conn);
            DataTable tmpdt1 = new DataTable();
            DataTable tmpdt2 = new DataTable();
            tmpsda1.Fill(tmpdt1);
            tmpsda2.Fill(tmpdt2);
            tab13_dgv1.DataSource = tmpdt1;
            tab13_dgv1.AutoResizeColumns();
            tab13_txtCount.Text = GetInt(tmpdt2.Rows[0]["总项数"]).ToString();
            tab13_txtQuantity.Text = GetInt(tmpdt2.Rows[0]["总数量"]).ToString();
            //例试计划总项数、总数量
            string strTmp1 = strTmpSelectString;
            string strTmp2 = strTmpSumString;
            strTmp1 += " and jhb023 ='例试'";
            strTmp2 += " and jhb023 ='例试'";
            tmpsda1 = new SqlDataAdapter(strTmp1, conn);
            tmpsda2 = new SqlDataAdapter(strTmp2, conn);
            tmpdt1 = new DataTable();
            tmpdt2 = new DataTable();
            tmpsda1.Fill(tmpdt1);
            tmpsda2.Fill(tmpdt2);
            tab13_dgv2.DataSource = tmpdt1;
            tab13_dgv2.AutoResizeColumns();
            tab13_txtCount_ls.Text = GetInt(tmpdt2.Rows[0]["总项数"]).ToString();
            tab13_txtQuantity_ls.Text = GetInt(tmpdt2.Rows[0]["总数量"]).ToString();
            //项目计划总项数、总数量
            strTmp1 = strTmpSelectString;
            strTmp2 = strTmpSumString;
            strTmp1 += " and jha005='036'";
            strTmp2 += " and jha005='036'";
            tmpsda1 = new SqlDataAdapter(strTmp1, conn);
            tmpsda2 = new SqlDataAdapter(strTmp2, conn);
            tmpdt1 = new DataTable();
            tmpdt2 = new DataTable();
            tmpsda1.Fill(tmpdt1);
            tmpsda2.Fill(tmpdt2);
            tab13_dgv3.DataSource = tmpdt1;
            tab13_dgv3.AutoResizeColumns();
            tab13_txtCount_xm.Text = GetInt(tmpdt2.Rows[0]["总项数"]).ToString();
            tab13_txtQuantity_xm.Text = GetInt(tmpdt2.Rows[0]["总数量"]).ToString();
            //送样计划总项数、总数量
            strTmp1 = strTmpSelectString;
            strTmp2 = strTmpSumString;
            strTmp1 += " and jhb023='送样'";
            strTmp2 += " and jhb023='送样'";
            tmpsda1 = new SqlDataAdapter(strTmp1, conn);
            tmpsda2 = new SqlDataAdapter(strTmp2, conn);
            tmpdt1 = new DataTable();
            tmpdt2 = new DataTable();
            tmpsda1.Fill(tmpdt1);
            tmpsda2.Fill(tmpdt2);
            tab13_dgv4.DataSource = tmpdt1;
            tab13_dgv4.AutoResizeColumns();
            tab13_txtCount_sy.Text = GetInt(tmpdt2.Rows[0]["总项数"]).ToString();
            tab13_txtQuantity_sy.Text = GetInt(tmpdt2.Rows[0]["总数量"]).ToString();
            Tools.CloseConnection(conn);
        }
        #endregion

        #region 生产计划跟踪
        private void tab14_btnQuery_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            SqlConnection tmpconn = Tools.GetConnection(strConnectionStringTP);
            SqlDataAdapter tmpsda;
            if (tab14_txtContractNo.Text.Trim().Equals(string.Empty)&&tab14_txtModel .Text .Trim ().Equals (string.Empty ))
            {
                MessageBox.Show("请先输入要查询的合同号或型号规格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tab14_txtContractNo.Focus();
                return;
            }
            string strTmpSelectString = tab14_SelectString;
            if (!tab14_txtContractNo.Text.Trim().Equals(string.Empty))
            {
                strTmpSelectString += " and jhb024 like '" + tab14_txtContractNo.Text.Trim() + "'";
            }
            if (!tab14_txtModel.Text.Trim().Equals(string.Empty))
            {
                strTmpSelectString += " and jhb005 like '" + tab14_txtModel.Text.Trim() + "'";
            }
            tmpsda = new SqlDataAdapter(tab14_SelectString.Replace("@合同号", tab14_txtContractNo.Text.Trim()), tmpconn);
            Tools.OpenConnection(tmpconn);
            tmpsda.Fill(dt);
            Tools.CloseConnection(tmpconn);
            tab14_dgv1.DataSource = dt;
            tab14_dgv1.AutoResizeColumns();
        }
        #endregion

        #region 指定芯片结余及提交清单
        private void tab15_btnQuery_Click(object sender, EventArgs e)
        {
            if (tab15_txtChipSN .Text .Trim ().Equals (string.Empty ))
            {
                MessageBox.Show("请输入芯片号！");
                tab15_txtChipSN.Focus();
                return;
            }
            tab15_txtChipSN.Text = tab15_txtChipSN.Text.ToUpper();
            string strTmpSelectJy_gx = tab15_SelectJy_gx;
            string strTmpSelectJy_bcp = tab15_SelectJy_bcp;
            string strTmpSelectJy_cp = tab15_SelectJy_cp;
            if (tab15_chkQueryLockedItems .Checked )
            {
                strTmpSelectJy_gx = strTmpSelectJy_gx.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                strTmpSelectJy_bcp = strTmpSelectJy_bcp.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                strTmpSelectJy_cp = strTmpSelectJy_cp.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
            }
            string strTmpSelectTj = tab15_SelectTj;
            string[] strChipSNs = tab15_txtChipSN.Text.Split(new char[] { ';' });
            for(int i=0;i<strChipSNs .Length;i++)
            {
                if (i==0)
                {
                    strTmpSelectJy_gx += " and (";
                    strTmpSelectJy_bcp += " and (";
                    strTmpSelectJy_cp += " and (";
                    strTmpSelectTj += " where (";
                }
                strTmpSelectJy_gx += "chipsn='" + strChipSNs[i] + "'";
                strTmpSelectJy_bcp += "chipsn='" + strChipSNs[i] + "'";
                strTmpSelectJy_cp += "chipsn='" + strChipSNs[i] + "'";
                strTmpSelectTj += "芯片号='" + strChipSNs[i] + "'";
                if (i<strChipSNs .Length -1)
                {
                    strTmpSelectJy_gx += " or ";
                    strTmpSelectJy_bcp += " or ";
                    strTmpSelectJy_cp += " or ";
                    strTmpSelectTj += " or ";
                }
                else
                {
                    strTmpSelectJy_gx += ")";
                    strTmpSelectJy_bcp += ")";
                    strTmpSelectJy_cp += ")";
                    strTmpSelectTj += ")";
                }
            }
            strTmpSelectJy_gx += " order by chipsn";
            strTmpSelectJy_bcp += " order by chipsn";
            strTmpSelectJy_cp += " order by chipsn";
            strTmpSelectTj += " order by 芯片号";
            DataTable tmpdt1 = new DataTable();
            DataTable tmpdt_gx = new DataTable();
            DataTable tmpdt_bcp = new DataTable();
            DataTable tmpdt_cp = new DataTable();
            DataTable tmpdt2 = new DataTable();
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlCommand cmd_gx = new SqlCommand(strTmpSelectJy_gx, conn);
            SqlCommand cmd_bcp = new SqlCommand(strTmpSelectJy_bcp, conn);
            SqlCommand cmd_cp = new SqlCommand(strTmpSelectJy_cp, conn);
            SqlCommand cmd2 = new SqlCommand(strTmpSelectTj, conn);
            SqlDataAdapter tmpda_gx = new SqlDataAdapter(cmd_gx);
            SqlDataAdapter tmpda_bcp = new SqlDataAdapter(cmd_bcp);
            SqlDataAdapter tmpda_cp = new SqlDataAdapter(cmd_cp);
            SqlDataAdapter tmpda2 = new SqlDataAdapter(cmd2);
            Tools.OpenConnection(conn);
            tmpda_gx.Fill(tmpdt_gx);
            tmpda_bcp.Fill(tmpdt_bcp);
            tmpda_cp.Fill(tmpdt_cp);
            tmpda2.Fill(tmpdt2);
            Tools.CloseConnection(conn);
            tmpdt1 = tmpdt_gx.Copy();
            foreach (DataRow dr in tmpdt_gx .Rows )
            {
                tmpdt1.ImportRow(dr);
            }
            foreach (DataRow dr in tmpdt_bcp.Rows)
            {
                tmpdt1.ImportRow(dr);
            }
            foreach (DataRow dr in tmpdt_cp.Rows)
            {
                tmpdt1.ImportRow(dr);
            }
            tab15_dgv1.DataSource = tmpdt1;
            tab15_dgv1.AutoResizeColumns();
            tab15_dgv2.DataSource = tmpdt2;
            tab15_dgv2.AutoResizeColumns();
        }

        private void tab15_btnExport_Click(object sender, EventArgs e)
        {
            if (tab15_txtChipSN.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入芯片号！");
                tab15_txtChipSN.Focus();
                return;
            }
            if (tab15_chkExportToSepareteFile.Checked)
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog ()==DialogResult.Cancel )
                {
                    return;
                }
                string strPath = fbd.SelectedPath + "\\";
                //按芯片号导出到单独的文件
                string strTmpSelectJy_gx = tab15_SelectJy_gx;
                string strTmpSelectJy_bcp = tab15_SelectJy_bcp;
                string strTmpSelectJy_cp = tab15_SelectJy_cp;
                if (tab15_chkQueryLockedItems.Checked)
                {
                    strTmpSelectJy_gx = strTmpSelectJy_gx.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                    strTmpSelectJy_bcp = strTmpSelectJy_bcp.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                    strTmpSelectJy_cp = strTmpSelectJy_cp.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                }
                string strTmpSelectTj = tab15_SelectTj;
                SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
                Tools.OpenConnection(conn);
                string[] strChipSNs = tab15_txtChipSN.Text.Split(new char[] { ';' });
                for (int i = 0; i < strChipSNs.Length; i++)
                {
                    DataTable tmpdt_gx = new DataTable();
                    DataTable tmpdt_bcp = new DataTable();
                    DataTable tmpdt_cp = new DataTable();
                    DataTable tmpdt2 = new DataTable();
                    if (strChipSNs [i].Trim ().Equals (string.Empty ))
                    {
                        continue;
                    }
                    string strFileName = strPath + strChipSNs[i] + ".xls";
                    SqlDataAdapter tmpsda_gx = new SqlDataAdapter(strTmpSelectJy_gx + " and chipsn ='" + strChipSNs[i] + "'", conn);
                    SqlDataAdapter tmpsda_bcp = new SqlDataAdapter(strTmpSelectJy_bcp + " and chipsn ='" + strChipSNs[i] + "'", conn);
                    SqlDataAdapter tmpsda_cp = new SqlDataAdapter(strTmpSelectJy_cp + " and chipsn ='" + strChipSNs[i] + "'", conn);
                    SqlDataAdapter tmpsda2 = new SqlDataAdapter(strTmpSelectTj + " where 芯片号 ='" + strChipSNs[i] + "'", conn);
                    tmpsda_gx.Fill(tmpdt_gx);
                    tmpsda_bcp.Fill(tmpdt_bcp);
                    tmpsda_cp.Fill(tmpdt_cp);
                    for (int k = 0; k < tmpdt_gx.Rows.Count; k++)
                    {
                        if (!GetString(tmpdt_gx.Rows[k]["货架号"]).Equals(string.Empty))
                        {
                            tmpdt_gx.Rows[k]["货架号"] = "'" + GetString(tmpdt_gx.Rows[k]["货架号"]);
                        }
                        tmpdt_gx.Rows[k]["工单号"] = "'" + GetString(tmpdt_gx.Rows[k]["工单号"]);
                    }
                    for (int k = 0; k < tmpdt_bcp.Rows.Count; k++)
                    {
                        if (!GetString(tmpdt_bcp.Rows[k]["货架号"]).Equals(string.Empty))
                        {
                            tmpdt_bcp.Rows[k]["货架号"] = "'" + GetString(tmpdt_bcp.Rows[k]["货架号"]);
                        }
                        tmpdt_bcp.Rows[k]["工单号"] = "'" + GetString(tmpdt_bcp.Rows[k]["工单号"]);
                    }
                    for (int k = 0; k < tmpdt_cp.Rows.Count; k++)
                    {
                        if (!GetString(tmpdt_cp.Rows[k]["货架号"]).Equals(string.Empty))
                        {
                            tmpdt_cp.Rows[k]["货架号"] = "'" + GetString(tmpdt_cp.Rows[k]["货架号"]);
                        }
                        tmpdt_cp.Rows[k]["工单号"] = "'" + GetString(tmpdt_cp.Rows[k]["工单号"]);
                    }
                    tmpsda2.Fill(tmpdt2);
                    tab15_ExportToExcel(strFileName, tmpdt_gx,tmpdt_bcp, tmpdt_cp, tmpdt2);
                }
                Tools.CloseConnection(conn);
                MessageBox.Show("导出已完成！");
            }
            else
            {
                //导出到一个文件
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "*.xls|*.xls";
                if (sfd.ShowDialog ()==DialogResult.Cancel )
                {
                    return;
                }
                string strFileName = sfd.FileName;
                string strTmpSelectJy_gx = tab15_SelectJy_gx;
                string strTmpSelectJy_bcp = tab15_SelectJy_bcp;
                string strTmpSelectJy_cp = tab15_SelectJy_cp;
                if (tab15_chkQueryLockedItems.Checked)
                {
                    strTmpSelectJy_gx = strTmpSelectJy_gx.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                    strTmpSelectJy_bcp = strTmpSelectJy_bcp.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                    strTmpSelectJy_cp = strTmpSelectJy_cp.Replace("gxjy008>0", "(gxjy008>0 or gxjy015 like '结余已锁定%')");
                }
                string strTmpSelectTj = tab15_SelectTj;
                string[] strChipSNs = tab15_txtChipSN.Text.Split(new char[] { ';' });
                for (int i = 0; i < strChipSNs.Length; i++)
                {
                    if (i == 0)
                    {
                        strTmpSelectJy_gx += " and (";
                        strTmpSelectJy_bcp += " and (";
                        strTmpSelectJy_cp += " and (";
                        strTmpSelectTj += " where (";
                    }
                    strTmpSelectJy_gx += "chipsn='" + strChipSNs[i] + "'";
                    strTmpSelectJy_bcp += "chipsn='" + strChipSNs[i] + "'";
                    strTmpSelectJy_cp += "chipsn='" + strChipSNs[i] + "'";
                    strTmpSelectTj += "芯片号='" + strChipSNs[i] + "'";
                    if (i < strChipSNs.Length - 1)
                    {
                        strTmpSelectJy_gx += " or ";
                        strTmpSelectJy_bcp += " or ";
                        strTmpSelectJy_cp += " or ";
                        strTmpSelectTj += " or ";
                    }
                    else
                    {
                        strTmpSelectJy_gx += ")";
                        strTmpSelectJy_bcp += ")";
                        strTmpSelectJy_cp += ")";
                        strTmpSelectTj += ")";
                    }
                }
                strTmpSelectJy_gx += " order by chipsn";
                strTmpSelectJy_bcp += " order by chipsn";
                strTmpSelectJy_cp += " order by chipsn";
                strTmpSelectTj += " order by 芯片号";
                DataTable tmpdt_gx = new DataTable();
                DataTable tmpdt_bcp = new DataTable();
                DataTable tmpdt_cp = new DataTable();
                DataTable tmpdt2 = new DataTable();
                SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
                SqlCommand cmd_gx = new SqlCommand(strTmpSelectJy_gx, conn);
                SqlCommand cmd_bcp = new SqlCommand(strTmpSelectJy_bcp, conn);
                SqlCommand cmd_cp = new SqlCommand(strTmpSelectJy_cp, conn);
                SqlCommand cmd2 = new SqlCommand(strTmpSelectTj, conn);
                SqlDataAdapter tmpda_gx = new SqlDataAdapter(cmd_gx);
                SqlDataAdapter tmpda_bcp = new SqlDataAdapter(cmd_bcp);
                SqlDataAdapter tmpda_cp = new SqlDataAdapter(cmd_cp);
                SqlDataAdapter tmpda2 = new SqlDataAdapter(cmd2);
                Tools.OpenConnection(conn);
                tmpda_gx.Fill(tmpdt_gx);
                tmpda_bcp.Fill(tmpdt_bcp);
                tmpda_cp.Fill(tmpdt_cp);
                for (int k = 0; k < tmpdt_gx.Rows.Count; k++)
                {
                    if (!GetString(tmpdt_gx.Rows[k]["货架号"]).Equals(string.Empty))
                    {
                        tmpdt_gx.Rows[k]["货架号"] = "'" + GetString(tmpdt_gx.Rows[k]["货架号"]);
                    }
                    tmpdt_gx.Rows[k]["工单号"] = "'" + GetString(tmpdt_gx.Rows[k]["工单号"]);
                }
                for (int k = 0; k < tmpdt_bcp.Rows.Count; k++)
                {
                    if (!GetString(tmpdt_bcp.Rows[k]["货架号"]).Equals(string.Empty))
                    {
                        tmpdt_bcp.Rows[k]["货架号"] = "'" + GetString(tmpdt_bcp.Rows[k]["货架号"]);
                    }
                    tmpdt_bcp.Rows[k]["工单号"] = "'" + GetString(tmpdt_bcp.Rows[k]["工单号"]);
                }
                for (int k = 0; k < tmpdt_cp.Rows.Count; k++)
                {
                    if (!GetString(tmpdt_cp.Rows[k]["货架号"]).Equals(string.Empty))
                    {
                        tmpdt_cp.Rows[k]["货架号"] = "'" + GetString(tmpdt_cp.Rows[k]["货架号"]);
                    }
                    tmpdt_cp.Rows[k]["工单号"] = "'" + GetString(tmpdt_cp.Rows[k]["工单号"]);
                }
                tmpda2.Fill(tmpdt2);
                Tools.CloseConnection(conn);
                tab15_ExportToExcel(strFileName, tmpdt_gx,tmpdt_bcp ,tmpdt_cp, tmpdt2);
                MessageBox.Show("导出已完成！");
            }
        }

        private bool tab15_ExportToExcel(string strTmpFileName, DataTable dtJy_gx, DataTable dtJy_bcp, DataTable dtJy_cp, DataTable dtTj)
        {
            if (dtJy_gx .Rows.Count ==0&& dtJy_bcp.Rows.Count == 0 && dtJy_cp.Rows.Count == 0 && dtTj .Rows.Count ==0)
            {
                return false;
            }
            try
            {
                int iCount = 0;
                Excel._Application tmpapp = new Excel.Application();
                Excel.Workbook tmpwb = tmpapp.Workbooks.Add(true);
                if (dtJy_gx .Rows.Count >0)
                {
                    iCount++;
                    tmpwb.Worksheets.Add();
                    Excel.Worksheet tmpwsJy = (Excel.Worksheet)tmpwb.Worksheets[1];
                    #region 工序结余
                    tmpwsJy.Name = "工序结余";
                    Excel.Range tmprange;
                    //内容
                    object[,] tmpobj = new object[dtJy_gx.Rows.Count+1, dtJy_gx.Columns.Count];
                    for (int i =0;i<= dtJy_gx.Rows.Count ; i++)
                    {
                        for (int k = 0; k < dtJy_gx.Columns.Count; k++)
                        {
                            if (i==0)
                            {
                                tmpobj[i, k] = dtJy_gx.Columns[k].ColumnName;
                            }
                            else
                            {
                                tmpobj[i, k] = dtJy_gx.Rows[i-1][k];
                            }
                        }
                    }
                    tmprange = tmpwsJy.get_Range("A1", tmpwsJy.Cells[dtJy_gx.Rows.Count + 1, dtJy_gx.Columns.Count]);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpobj;
                    #endregion
                }
                if (dtJy_bcp.Rows.Count > 0)
                {
                    iCount++;
                    tmpwb.Worksheets.Add();
                    Excel.Worksheet tmpwsJy = (Excel.Worksheet)tmpwb.Worksheets[1];
                    #region 半成品结余
                    tmpwsJy.Name = "半成品结余";
                    Excel.Range tmprange;
                    //内容
                    object[,] tmpobj = new object[dtJy_bcp.Rows.Count + 1, dtJy_bcp.Columns.Count];
                    for (int i = 0; i <= dtJy_bcp.Rows.Count; i++)
                    {
                        for (int k = 0; k < dtJy_bcp.Columns.Count; k++)
                        {
                            if (i == 0)
                            {
                                tmpobj[i, k] = dtJy_bcp.Columns[k].ColumnName;
                            }
                            else
                            {
                                tmpobj[i, k] = dtJy_bcp.Rows[i - 1][k];
                            }
                        }
                    }
                    tmprange = tmpwsJy.get_Range("A1", tmpwsJy.Cells[dtJy_bcp.Rows.Count + 1, dtJy_bcp.Columns.Count]);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpobj;
                    #endregion
                }
                if (dtJy_cp.Rows.Count > 0)
                {
                    iCount++;
                    tmpwb.Worksheets.Add();
                    Excel.Worksheet tmpwsJy = (Excel.Worksheet)tmpwb.Worksheets[1];
                    #region 成品结余
                    tmpwsJy.Name = "成品结余";
                    Excel.Range tmprange;
                    //内容
                    object[,] tmpobj = new object[dtJy_cp.Rows.Count + 1, dtJy_cp.Columns.Count];
                    for (int i = 0; i <= dtJy_cp.Rows.Count; i++)
                    {
                        for (int k = 0; k < dtJy_cp.Columns.Count; k++)
                        {
                            if (i == 0)
                            {
                                tmpobj[i, k] = dtJy_cp.Columns[k].ColumnName;
                            }
                            else
                            {
                                tmpobj[i, k] = dtJy_cp.Rows[i - 1][k];
                            }
                        }
                    }
                    tmprange = tmpwsJy.get_Range("A1", tmpwsJy.Cells[dtJy_cp.Rows.Count + 1, dtJy_cp.Columns.Count]);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpobj;
                    #endregion
                }
                if (dtTj.Rows.Count >0)
                {
                    iCount++;
                    tmpwb.Worksheets.Add();
                    Excel.Worksheet tmpwsTj = (Excel.Worksheet)tmpwb.Worksheets[1];
                    #region 提交
                    Excel.Range tmprange;
                    tmpwsTj.Name = "提交清单";
                    //内容
                    object[,] tmpobj = new object[dtTj.Rows.Count+1, dtTj.Columns.Count];
                    for (int i = 0; i <= dtTj.Rows.Count; i++)
                    {
                        for (int k = 0; k < dtTj.Columns.Count; k++)
                        {
                            if (i == 0)
                            {
                                tmpobj[i, k] = dtTj.Columns[k].ColumnName;
                            }
                            else
                            {
                                tmpobj[i, k] = dtTj.Rows[i-1][k];
                            }
                        }
                    }
                    tmprange = tmpwsTj.get_Range("A1", tmpwsTj.Cells[dtTj.Rows.Count + 1, dtTj.Columns.Count]);
                    tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                    tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                    tmprange.Value2 = tmpobj;
                    #endregion
                }
                if (tmpwb .Worksheets .Count >iCount )
                {
                    ((Excel.Worksheet)tmpwb.Worksheets[tmpwb.Worksheets.Count]).Delete();
                }
                tmpwb.SaveAs(strTmpFileName, Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                tmpapp.Quit();
                if (tmpapp != null)
                {
                    IntPtr t = new IntPtr(tmpapp.Hwnd);
                    int k = 0;
                    GetWindowThreadProcessId(t, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion

        #region 合同信息导出
        private void tab16_btnAddCustomerID_Click(object sender, EventArgs e)
        {
            if (tab16_txtCustomerID .Text .Trim ().Equals (string.Empty ))
            {
                MessageBox.Show("客户编码不能为空！");
                tab16_txtCustomerID.Focus();
                return;
            }
            if (tab16_lstCustomerID .FindString (tab16_txtCustomerID .Text.Trim () )>-1)
            {
                return;
            }
            tab16_lstCustomerID.Items.Add(tab16_txtCustomerID.Text.Trim());
        }

        private void tab16_btnAddCustomerArea_Click(object sender, EventArgs e)
        {
            if (tab16_txtCustomerArea.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("客户片区不能为空！");
                tab16_txtCustomerArea.Focus();
                return;
            }
            if (tab16_lstCustomerArea.FindString(tab16_txtCustomerArea.Text.Trim()) > -1)
            {
                return;
            }
            tab16_lstCustomerArea.Items.Add(tab16_txtCustomerArea.Text.Trim());
        }

        private void tab16_btnAddContractNo_Click(object sender, EventArgs e)
        {
            if(tab16_txtContractNo.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("合同号不能为空！");
                tab16_txtContractNo.Focus();
                return;
            }
            if (tab16_lstContractNo.FindString(tab16_txtContractNo.Text.Trim()) > -1)
            {
                return;
            }
            tab16_lstContractNo.Items.Add(tab16_txtContractNo.Text.Trim());
        }

        private void tab16_lstCustomerID_DoubleClick(object sender, EventArgs e)
        {
            if (tab16_lstCustomerID .SelectedIndex >=0)
            {
                tab16_lstCustomerID.Items.RemoveAt(tab16_lstCustomerID.SelectedIndex);
            }
        }

        private void tab16_lstCustomerArea_DoubleClick(object sender, EventArgs e)
        {
            if (tab16_lstCustomerArea.SelectedIndex >= 0)
            {
                tab16_lstCustomerArea.Items.RemoveAt(tab16_lstCustomerArea.SelectedIndex);
            }
        }

        private void tab16_lstContractNo_DoubleClick(object sender, EventArgs e)
        {
            if (tab16_lstContractNo.SelectedIndex >= 0)
            {
                tab16_lstContractNo.Items.RemoveAt(tab16_lstContractNo.SelectedIndex);
            }

        }

        private void tab16_chklstbSummary_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.Index >=0&&e.CurrentValue ==CheckState.Unchecked)
            {
                //如果当前状态为未选中，则新状态为已选中，将该项加入到字段列表中
                tab16_chklstbField.Items.Add(tab16_chklstbSummary.Items[e.Index].ToString(),true);
            }
            else
            {
                //如果当前状态为已选中，则新状态为未选中，将该项从字段列表中删除
                tab16_chklstbField.Items.Remove(tab16_chklstbSummary.Items[e.Index].ToString());
            }
        }

        private void tab16_chklstbField_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.Index >=0)
            {
                //如果变更选中状态的项是汇总列表中的项，则总是设置该项为选中状态
                if (tab16_chklstbSummary.FindString (tab16_chklstbField.Items[e.Index].ToString ())>0)
                {
                    e.NewValue = CheckState.Checked;
                }
            }
        }

        private void tab16_chkDhrq_CheckedChanged(object sender, EventArgs e)
        {
            tab16_dtpFrom_dhrq.Enabled  = tab16_chkDhrq.Checked;
            tab16_dtpTo_dhrq.Enabled = tab16_chkDhrq.Checked;
        }

        private void tab16_chkJhrq_CheckedChanged(object sender, EventArgs e)
        {
            tab16_dtpFrom_jhrq.Enabled = tab16_chkJhrq.Checked;
            tab16_dtpTo_jhrq.Enabled = tab16_chkJhrq.Checked;
        }

        private void tab16_chkFhrq_CheckedChanged(object sender, EventArgs e)
        {
            tab16_dtpFrom_fhrq.Enabled = tab16_chkFhrq.Checked;
            tab16_dtpTo_fhrq.Enabled = tab16_chkFhrq.Checked;
        }

        private void tab16_chkKprq_CheckedChanged(object sender, EventArgs e)
        {
            tab16_dtpFrom_kprq.Enabled = tab16_chkKprq.Checked;
            tab16_dtpTo_kprq.Enabled = tab16_chkKprq.Checked;
        }

        private void tab16_btnSummaryUp_Click(object sender, EventArgs e)
        {
            //调整汇总项向上
            if (tab16_chklstbSummary.SelectedIndex >0)
            {
                //保存项选中状态
                CheckState cs1 = tab16_chklstbSummary.GetItemCheckState (tab16_chklstbSummary.SelectedIndex);
                CheckState cs2 = tab16_chklstbSummary.GetItemCheckState(tab16_chklstbSummary.SelectedIndex-1);
                //保存项值
                object tmpobj1 = tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex];
                object tmpobj2 = tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex - 1];
                //交换值
                tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex] = tmpobj2;
                tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex - 1] = tmpobj1;
                //设置选中状态
                tab16_chklstbSummary.SetItemCheckState(tab16_chklstbSummary.SelectedIndex, cs2);
                tab16_chklstbSummary.SetItemCheckState(tab16_chklstbSummary.SelectedIndex-1, cs1);
                //设置当前项
                tab16_chklstbSummary.SelectedIndex = tab16_chklstbSummary.SelectedIndex - 1;
            }
        }

        private void tab16_btnSummaryDown_Click(object sender, EventArgs e)
        {
            //调整汇总项向下
            if (tab16_chklstbSummary.SelectedIndex >= 0&&tab16_chklstbSummary .SelectedIndex < tab16_chklstbSummary.Items.Count -1)
            {
                //保存项选中状态
                CheckState cs1 = tab16_chklstbSummary.GetItemCheckState(tab16_chklstbSummary.SelectedIndex);
                CheckState cs2 = tab16_chklstbSummary.GetItemCheckState(tab16_chklstbSummary.SelectedIndex + 1);
                //保存项值
                object tmpobj1 = tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex];
                object tmpobj2 = tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex + 1];
                //交换值
                tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex] = tmpobj2;
                tab16_chklstbSummary.Items[tab16_chklstbSummary.SelectedIndex + 1] = tmpobj1;
                //设置选中状态
                tab16_chklstbSummary.SetItemCheckState(tab16_chklstbSummary.SelectedIndex, cs2);
                tab16_chklstbSummary.SetItemCheckState(tab16_chklstbSummary.SelectedIndex + 1, cs1);
                //设置当前项
                tab16_chklstbSummary.SelectedIndex = tab16_chklstbSummary.SelectedIndex + 1;
            }
        }

        private void tab16_btnFieldUp_Click(object sender, EventArgs e)
        {
            //调整汇总项向上
            if (tab16_chklstbField.SelectedIndex > 0)
            {
                //保存项选中状态
                CheckState cs1 = tab16_chklstbField.GetItemCheckState(tab16_chklstbField.SelectedIndex);
                CheckState cs2 = tab16_chklstbField.GetItemCheckState(tab16_chklstbField.SelectedIndex - 1);
                //保存项值
                object tmpobj1 = tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex];
                object tmpobj2 = tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex - 1];
                //交换值
                tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex] = tmpobj2;
                tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex - 1] = tmpobj1;
                //设置选中状态
                tab16_chklstbField.SetItemCheckState(tab16_chklstbField.SelectedIndex, cs2);
                tab16_chklstbField.SetItemCheckState(tab16_chklstbField.SelectedIndex - 1, cs1);
                //设置当前项
                tab16_chklstbField.SelectedIndex = tab16_chklstbField.SelectedIndex - 1;
            }
        }

        private void tab16_btnFieldDown_Click(object sender, EventArgs e)
        {
            //调整汇总项向下
            if (tab16_chklstbField.SelectedIndex >= 0 && tab16_chklstbField.SelectedIndex < tab16_chklstbField.Items.Count - 1)
            {
                //保存项选中状态
                CheckState cs1 = tab16_chklstbField.GetItemCheckState(tab16_chklstbField.SelectedIndex);
                CheckState cs2 = tab16_chklstbField.GetItemCheckState(tab16_chklstbField.SelectedIndex + 1);
                //保存项值
                object tmpobj1 = tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex];
                object tmpobj2 = tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex + 1];
                //交换值
                tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex] = tmpobj2;
                tab16_chklstbField.Items[tab16_chklstbField.SelectedIndex + 1] = tmpobj1;
                //设置选中状态
                tab16_chklstbField.SetItemCheckState(tab16_chklstbField.SelectedIndex, cs2);
                tab16_chklstbField.SetItemCheckState(tab16_chklstbField.SelectedIndex + 1, cs1);
                //设置当前项
                tab16_chklstbField.SelectedIndex = tab16_chklstbField.SelectedIndex + 1;
            }
        }

        private void tab16_btnQuery_Click(object sender, EventArgs e)
        {
            if (tab16_chklstbField .CheckedItems .Count ==0)
            {
                MessageBox.Show("请先选择要查询的字段！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //是否查询发货
            bool bQueryFh = false;
            //是否查询开票
            bool bQueryKp = false;
            //枚举字段，初始化查询脚本，组合查询字段
            string strTmpSelectString = "select ";
            for (int i=0;i<tab16_chklstbField .Items .Count;i++)
            {
                if (!tab16_chklstbField.GetItemChecked (i))
                {
                    continue;
                }
                switch (tab16_chklstbField.Items[i].ToString ())
                {
                    case "客户编号":
                        strTmpSelectString += "hta003 客户编号";
                        break;
                    case "客户名称":
                        strTmpSelectString += "hta004 客户名称";
                        break;
                    case "客户片区":
                        strTmpSelectString += "ku007 客户片区";
                        break;
                    case "合同号":
                        strTmpSelectString += "hta011 合同号";
                        break;
                    case "订货日期":
                        strTmpSelectString += "hta002 订货日期";
                        break;
                    case "发货日期":
                        strTmpSelectString += "fh.发货日期 发货日期";
                        bQueryFh = true; 
                        break;
                    case "开票日期":
                        strTmpSelectString += "kp.开票日期 开票日期";
                        bQueryKp = true;
                        break;
                    case "运输单号":
                        strTmpSelectString += "fh.运输单号 运输单号";
                        bQueryFh = true;
                        break;
                    case "发票号码":
                        strTmpSelectString += "kp.发票号码 发票号码";
                        bQueryKp = true;
                        break;
                    case "订货总数量":
                        strTmpSelectString += "sum(htb006) 订货总数量";
                        break;
                    case "订货总金额":
                        strTmpSelectString += "sum(htb006*htb008) 订货总金额";
                        break;
                    case "发货总数量":
                        strTmpSelectString += "sum(fh.发货数量) 发货总数量";
                        bQueryFh = true;
                        break;
                    case "发货总金额":
                        strTmpSelectString += "sum(fh.发货金额) 发货总金额";
                        bQueryFh = true;
                        break;
                    case "开票总数量":
                        strTmpSelectString += "sum(kp.开票数量) 开票总数量";
                        bQueryKp = true;
                        break;
                    case "开票总金额":
                        strTmpSelectString += "sum(kp.开票金额) 开票总金额";
                        bQueryKp = true;
                        break;
                    case "分厂":
                        strTmpSelectString += "hta026 分厂";
                        break;
                    case "型号规格":
                        strTmpSelectString += "htb003 型号规格";
                        break;
                    case "订货数量":
                        strTmpSelectString += "htb006 订货数量";
                        break;
                    case "单价":
                        strTmpSelectString += "htb008 单价";
                        break;
                    case "订货金额":
                        strTmpSelectString += "htb009 订货金额";
                        break;
                    case "交货日期":
                        strTmpSelectString += "htb010 交货日期";
                        break;
                    case "执行标准":
                        strTmpSelectString += "htb021 执行标准";
                        break;
                    case "客户类型":
                        strTmpSelectString += "ku008 客户类型";
                        break;
                    case "验收方式":
                        strTmpSelectString += "hta029 验收方式";
                        break;
                    case "发票类型":
                        strTmpSelectString += "hta020 发票类型";
                        break;
                }
                if (i<tab16_chklstbField.Items.Count -1)
                {
                    strTmpSelectString += " , ";
                }
            }
            while (strTmpSelectString .EndsWith (" , "))
            {
                strTmpSelectString = strTmpSelectString.Substring(0, strTmpSelectString.Length - 3);
            }
            //检查时间查询条件，是否附加发货和开票表
            if (tab16_chkFhrq.Checked )
            {
                bQueryFh = true;
            }
            if (tab16_chkKprq .Checked )
            {
                bQueryKp = true;
            }
            //拼接表
            strTmpSelectString += " From ku_table,hta_table,htb_table";
            if (bQueryFh )
            {
                strTmpSelectString += " left join (select xhb033 订单编号,xhb034 订单项次,xha002 发货日期,xha021 运输单号,SUM(xhb008) 发货数量,SUM(xhb012) 发货金额 from orxha_table,orxhb_table where xha001=xhb001 and xha013='Y' group by xhb033,xhb034,xha002 ,xha021) fh on fh.订单编号=htb001 and fh.订单项次=htb002";
            }
            if (bQueryKp)
            {
                strTmpSelectString += " left join (select fpb005 订单编号,fpb006 订单项次,fpa002 开票日期,fpa012 发票号码,SUM(fpb012) 开票数量,SUM(fpb016) 开票金额 from invfpa_table ,invfpb_table where fpa001 =fpb001 and fpa019='Y' group by fpb005,fpb006,fpa002,fpa012) kp on kp.订单编号=htb001 and kp.订单项次=htb002";
            }
            //是否已有查询条件
            bool bIsKeyExist = false;
            //拼接查询条件
            //基本查询条件
            strTmpSelectString += " where hta001=htb001 and hta003=ku001 and hta014='Y'";
            //客户编号
            if (tab16_lstCustomerID.Items.Count >0)
            {
                strTmpSelectString += " and (";
                for(int i=0;i<tab16_lstCustomerID .Items .Count;i++)
                {
                    strTmpSelectString += "hta003 like '" + tab16_lstCustomerID.Items[i].ToString() + "'";
                    if (i<tab16_lstCustomerID .Items.Count -1)
                    {
                        strTmpSelectString += " or ";
                    }
                }
                strTmpSelectString += ")";
                bIsKeyExist = true;
            }
            //客户片区
            if (tab16_lstCustomerArea.Items.Count > 0)
            {
                strTmpSelectString += " and (";
                for (int i = 0; i < tab16_lstCustomerArea.Items.Count; i++)
                {
                    strTmpSelectString += "ku007 like '" + tab16_lstCustomerArea.Items[i].ToString() + "'";
                    if (i < tab16_lstCustomerArea.Items.Count - 1)
                    {
                        strTmpSelectString += " or ";
                    }
                }
                strTmpSelectString += ")";
                bIsKeyExist = true;
            }
            //合同号
            if (tab16_lstContractNo.Items.Count > 0)
            {
                strTmpSelectString += " and (";
                for (int i = 0; i < tab16_lstContractNo.Items.Count; i++)
                {
                    strTmpSelectString += "hta011 like '" + tab16_lstContractNo.Items[i].ToString() + "'";
                    if (i < tab16_lstContractNo.Items.Count - 1)
                    {
                        strTmpSelectString += " or ";
                    }
                }
                strTmpSelectString += ")";
                bIsKeyExist = true;
            }
            //发货日期
            if (tab16_chkDhrq .Checked )
            {
                strTmpSelectString += " and hta002 between '" + tab16_dtpFrom_dhrq.Value.ToString("yyyyMMdd") + "' and '"+ tab16_dtpTo_dhrq.Value.ToString("yyyyMMdd")+"'";
            }
            //交货日期
            if (tab16_chkJhrq.Checked)
            {
                strTmpSelectString += " and htb010 between '" + tab16_dtpFrom_jhrq.Value.ToString("yyyyMMdd") + "' and '" + tab16_dtpTo_jhrq.Value.ToString("yyyyMMdd") + "'";
            }
            //发货日期
            if (tab16_chkFhrq.Checked)
            {
                strTmpSelectString += " and fh.发货日期 between '" + tab16_dtpFrom_fhrq.Value.ToString("yyyyMMdd") + "' and '" + tab16_dtpTo_fhrq.Value.ToString("yyyyMMdd") + "'";
            }
            //开票日期
            if (tab16_chkKprq.Checked)
            {
                strTmpSelectString += " and kp.开票日期 between '" + tab16_dtpFrom_kprq.Value.ToString("yyyyMMdd") + "' and '" + tab16_dtpTo_kprq.Value.ToString("yyyyMMdd") + "'";
            }
            //分厂
            if (!tab16_cmbFactory .Text .Equals ("全部"))
            {
                switch (tab16_cmbFactory .Text )
                {
                    case "片容厂":
                        strTmpSelectString += " and hta026='603车间";
                        break;
                    case "特种厂":
                        strTmpSelectString += " and hta026='601车间";
                        break;
                }
            }
            //去除周期试验、为0的项次、合并的订单
            strTmpSelectString += " and hta011 not like '%宏科%' and htb006>0 and hta001 not like 'HB%'";
            //拼接Group by
            if (tab16_chklstbSummary.CheckedItems .Count >0)
            {
                strTmpSelectString += " group by ";
                for(int i=0;i<tab16_chklstbField .CheckedItems .Count;i++)
                {
                    if (tab16_chklstbSummary.FindString (tab16_chklstbField.CheckedItems[i].ToString ())==-1)
                    {
                        switch (tab16_chklstbField.CheckedItems[i].ToString())
                        {
                            case "客户编号":
                                strTmpSelectString += "hta003";
                                break;
                            case "客户名称":
                                strTmpSelectString += "hta004";
                                break;
                            case "客户片区":
                                strTmpSelectString += "ku007";
                                break;
                            case "合同号":
                                strTmpSelectString += "hta011";
                                break;
                            case "订货日期":
                                strTmpSelectString += "hta002";
                                break;
                            case "发货日期":
                                strTmpSelectString += "fh.发货日期";
                                break;
                            case "开票日期":
                                strTmpSelectString += "kp.开票日期";
                                break;
                            case "运输单号":
                                strTmpSelectString += "fh.运输单号";
                                break;
                            case "发票号码":
                                strTmpSelectString += "kp.发票号码";
                                break;
                            case "分厂":
                                strTmpSelectString += "hta026";
                                break;
                            case "型号规格":
                                strTmpSelectString += "htb003";
                                break;
                            case "订货数量":
                                strTmpSelectString += "htb006";
                                break;
                            case "单价":
                                strTmpSelectString += "htb008";
                                break;
                            case "订货金额":
                                strTmpSelectString += "htb009";
                                break;
                            case "交货日期":
                                strTmpSelectString += "htb010";
                                break;
                            case "执行标准":
                                strTmpSelectString += "htb021";
                                break;
                            case "客户类型":
                                strTmpSelectString += "ku008";
                                break;
                            case "验收方式":
                                strTmpSelectString += "hta029";
                                break;
                            case "发票类型":
                                strTmpSelectString += "hta020";
                                break;
                        }
                        if (i < tab16_chklstbField.CheckedItems.Count - 1)
                        {
                            strTmpSelectString += ",";
                        }
                    }
                }
            }
            if(strTmpSelectString .EndsWith (","))
            {
                strTmpSelectString = strTmpSelectString.Substring(0, strTmpSelectString.Length - 1);
            }
            //开始查询
            DataTable tmpdt = new DataTable();
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlCommand cmd = new SqlCommand(strTmpSelectString , conn);
            SqlDataAdapter tmpda = new SqlDataAdapter(cmd);
            Tools.OpenConnection(conn);
            tmpda.Fill(tmpdt);
            Tools.CloseConnection(conn);
            tab16_dgv1.DataSource = tmpdt;
            tab16_dgv1.AutoResizeColumns();
            MessageBox.Show(tab16_dgv1.Rows.Count.ToString() + "条记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void tab16_btnExport_Click(object sender, EventArgs e)
        {
            if(tab16_dgv1.DataSource ==null)
            {
                MessageBox.Show("没有可导出的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.xls|*.xls";
            if (sfd.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            string strFileName = sfd.FileName;
            DataTable tmpdt = (DataTable ) tab16_dgv1.DataSource;
            tab16_ExportToExcel(strFileName, tmpdt);
            MessageBox.Show("导出已完成！");
        }

        private bool tab16_ExportToExcel(string strTmpFileName, DataTable tmpdt)
        {
            if (tmpdt.Rows.Count == 0)
            {
                return false;
            }
            try
            {
                Excel._Application tmpapp = new Excel.Application();
                Excel.Workbook tmpwb = tmpapp.Workbooks.Add(true);
                tmpwb.Worksheets.Add();
                Excel.Worksheet tmpwsJy = (Excel.Worksheet)tmpwb.Worksheets[1];
                #region 工序结余
                tmpwsJy.Name = "数据";
                Excel.Range tmprange;
                //内容
                object[,] tmpobj = new object[tmpdt.Rows.Count + 1, tmpdt.Columns.Count];
                for (int i = 0; i <= tmpdt.Rows.Count; i++)
                {
                    for (int k = 0; k < tmpdt.Columns.Count; k++)
                    {
                        if (i == 0)
                        {
                            tmpobj[i, k] = tmpdt.Columns[k].ColumnName;
                        }
                        else
                        {
                            if (tmpdt.Columns[k].ColumnName.Equals("发票号码"))
                            {
                                tmpobj[i, k] = "'" + GeneralTools.GetString(tmpdt.Rows[i - 1][k], string.Empty);
                            }
                            else
                            {
                                tmpobj[i, k] = tmpdt.Rows[i - 1][k];
                            }
                        }
                    }
                }
                tmprange = tmpwsJy.get_Range("A1", tmpwsJy.Cells[tmpdt.Rows.Count + 1, tmpdt.Columns.Count]);
                tmprange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tmprange.HorizontalAlignment = Excel.Constants.xlCenter;
                tmprange.Value2 = tmpobj;
                #endregion
                tmpwb.SaveAs(strTmpFileName, Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                tmpapp.Quit();
                if (tmpapp != null)
                {
                    IntPtr t = new IntPtr(tmpapp.Hwnd);
                    int k = 0;
                    GetWindowThreadProcessId(t, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion

        #region 成品出库超期查询
        private void tab17_btnQuery_Click(object sender, EventArgs e)
        {
            string strTmpSelect = "select * from (select gxck001 出库单号, hta004 客户名称, hta011 合同号, gxck012 型号规格, gxck015 出库数量, gxck041 出库时间, jsqra009 包装接收确认时间, jsqra023 交库时间, jsqra015 入市场部库房时间,hta029 验收方式, DATEDIFF(day, gxck041, GETDATE()) 间隔天数,备注,已处理 from gxck_table left join hta_table on hta001 = gxck010 left join htb_table on htb001=gxck010 and htb002=gxck011 left join ckjsqr_table on gxck001 = jsqr007 left join ckjsqra_table on jsqra001 = jsqr001 and jsqra007 = 'Y' left join gxckTrackMemo on 出库单号=gxck001 where gxck016 = 'Y' and hta029 not like '%下%' and htb006>0 and gxck010 <> '' and gxck010 is not null and jsqra015 is null and gxck041 between '@起始日期' and '@截止日期' union all select gxck001 出库单号, hta004 客户名称, hta011 合同号, gxck012 型号规格, gxck015 出库数量, gxck043 出库时间, jsqra009 包装接收确认时间, jsqra023 交库时间, jsqra015 入市场部库房时间,hta029 验收方式, DATEDIFF(day, gxck043, GETDATE()) 间隔天数 ,备注,已处理 from gxck_table left join hta_table on hta001 = gxck010 left join htb_table on htb001=gxck010 and htb002=gxck011 left join ckjsqr_table on gxck001 = jsqr007 left join ckjsqra_table on jsqra001 = jsqr001 and jsqra007 = 'Y' left join gxckTrackMemo on 出库单号=gxck001 where gxck016 = 'K' and hta029 not like '%下%' and htb006>0 and gxck010 <> '' and gxck010 is not null and jsqra015 is null and gxck043 between  '@起始日期' and '@截止日期') a where a.合同号 not like '%宏科%' and a.间隔天数 >=@间隔天数";
            strTmpSelect = strTmpSelect.Replace("@起始日期", tab17_dtpFrom.Value.ToString("yyyy-MM-dd"))
                                                        .Replace("@截止日期", tab17_dtpTo.Value.ToString("yyyy-MM-dd"))
                                                        .Replace("@间隔天数", tab17_nud1.Value .ToString ());
            if (!tab17_txtContractNo .Text .Trim ().Equals (string.Empty ))
            {
                strTmpSelect += " and a.合同号 like '" + tab17_txtContractNo.Text.Trim() + "'";
            }
            if (!tab17_chkIncludeSimple .Checked )
            {
                strTmpSelect += " and a.合同号 not like '%送样%'";
            }
            strTmpSelect += " order by a.间隔天数 desc";
            SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
            SqlCommand cmd = new SqlCommand(strTmpSelect, conn);
            cmd.CommandTimeout = 120;
            SqlDataAdapter tmpda = new SqlDataAdapter(cmd);
            tmpda.SelectCommand.CommandTimeout = 120;
            Tools.OpenConnection(conn);
            DataTable tmpdt = new DataTable();
            tmpda.Fill(tmpdt);
            Tools.CloseConnection(conn);
            tab17_dgv1.DataSource = tmpdt;
            tab17_dgv1.AutoResizeColumns();
        }

        private void tab17_dgv1_DoubleClick(object sender, EventArgs e)
        {
            if (tab17_dgv1.CurrentRow != null)
            {
                string strOriginalMemo = GeneralTools.GetString(tab17_dgv1.CurrentRow.Cells["备注"].Value, string.Empty);
                string strGxckID= GeneralTools.GetString(tab17_dgv1.CurrentRow.Cells["出库单号"].Value, string.Empty);
                string strTmp = string.Empty;
                if (!strOriginalMemo .Equals (string.Empty ))
                {
                    strTmp = InputBox.ShowInputBox("输入", "请输入备注", strOriginalMemo);
                }
                else 
                {
                    strTmp = InputBox.ShowInputBox("输入", "请输入备注");
                }
                if(strTmp .Equals (string.Empty )||strTmp .Equals (strOriginalMemo ))
                {
                    return;
                }
                SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
                SqlCommand cmd = new SqlCommand("select * from gxckTrackMemo where 出库单号='"+strGxckID +"'", conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable tmpdt = new DataTable();
                Tools.OpenConnection(conn);
                sda.Fill(tmpdt);
                if (tmpdt.Rows.Count  >0)
                {
                    cmd.CommandText  = "update gxckTrackMemo set 备注='" + strTmp + "' where 出库单号='" + strGxckID + "'";
                    cmd.ExecuteNonQuery();
                }
                else 
                {
                    cmd.CommandText  = "insert into gxckTrackMemo (出库单号,备注) values ('"+strGxckID +"','" + strTmp + "')";
                    cmd.ExecuteNonQuery();
                }
                Tools.CloseConnection(conn);
                tab17_dgv1.CurrentRow.Cells["备注"].Value = strTmp;
            }
        }

        private void 添加备注ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tab17_dgv1_DoubleClick(this, new EventArgs());
        }

        private void 清除备注ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tab17_dgv1.CurrentRow != null)
            {
                string strGxckID = GeneralTools.GetString(tab17_dgv1.CurrentRow.Cells["出库单号"].Value, string.Empty);
                SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
                SqlCommand cmd = new SqlCommand("select * from gxckTrackMemo where 出库单号='" + strGxckID + "'", conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable tmpdt = new DataTable();
                Tools.OpenConnection(conn);
                sda.Fill(tmpdt);
                if (tmpdt.Rows.Count  > 0)
                {
                    cmd.CommandText  = "update gxckTrackMemo set 备注='' where 出库单号='" + strGxckID + "'";
                    cmd.ExecuteNonQuery();
                }
                Tools.CloseConnection(conn);
                tab17_dgv1.CurrentRow.Cells["备注"].Value = string.Empty ;
            }
        }

        private void 已处理ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (tab17_dgv1.CurrentRow != null)
            {
                string strGxckID = GeneralTools.GetString(tab17_dgv1.CurrentRow.Cells["出库单号"].Value, string.Empty);
                SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
                SqlCommand cmd = new SqlCommand("select * from gxckTrackMemo where 出库单号='" + strGxckID + "'", conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable tmpdt = new DataTable();
                Tools.OpenConnection(conn);
                sda.Fill(tmpdt);
                if (tmpdt.Rows.Count  > 0)
                {
                    cmd.CommandText  = "update gxckTrackMemo set 已处理=1 where 出库单号='" + strGxckID + "'";
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    cmd.CommandText  = "insert into gxckTrackMemo (出库单号,备注,已处理) values ('" + strGxckID + "','',1)";
                    cmd.ExecuteNonQuery();
                }
                Tools.CloseConnection(conn);
                tab17_dgv1.CurrentRow.Cells["已处理"].Value = true ;
            }
        }

        private void 未处理ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (tab17_dgv1.CurrentRow != null)
            {
                string strGxckID = GeneralTools.GetString(tab17_dgv1.CurrentRow.Cells["出库单号"].Value, string.Empty);
                SqlConnection conn = Tools.GetConnection(strConnectionStringTP);
                SqlCommand cmd = new SqlCommand("select * from gxckTrackMemo where 出库单号='" + strGxckID + "'", conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable tmpdt = new DataTable();
                Tools.OpenConnection(conn);
                sda.Fill(tmpdt);
                if (tmpdt.Rows.Count > 0)
                {
                    cmd.CommandText  = "update gxckTrackMemo set 已处理=0 where 出库单号='" + strGxckID + "'";
                    cmd.ExecuteNonQuery();
                }
                Tools.CloseConnection(conn);
                tab17_dgv1.CurrentRow.Cells["已处理"].Value = false ;
            }
        }


        #endregion


    }
}